Sriram
Sriram

Reputation: 1190

How to delete rows from the datastore that our app uses in google appengine?

All the videos and tutorials show only how to add stuff to the datastore and fetch through the GqlQuery. How to delete stuff from the datastore? Does a 'DELETE FROM mytable WHERE condition' query work?

Upvotes: 4

Views: 3620

Answers (2)

systempuntoout
systempuntoout

Reputation: 74064

GQL language is read-only and it is useful for retrieving entities or keys from the App Engine Datastore.

For the DELETE Statements you have to use the delete method provided by the Datastore API.
An example:

q = db.GqlQuery("SELECT * FROM Foo")
results = q.fetch(10)
for result in results:
    result.delete()

Upvotes: 1

bgporter
bgporter

Reputation: 36474

The answers by systempuntout and Forest are both correct -- however, you may want to know that you can also batch some datastore operations together for higher efficiency (see this blog post from the App Engine team).

You could modify systempuntout's response like this:

q = db.GqlQuery("SELECT * FROM Foo")
results = q.fetch(10)
db.delete(results)

Instead of a roundtrip for each item deleted, it's a single operation to delete the lot of them.

Upvotes: 6

Related Questions