How to delete a record using GQL?

J.Olufsen picture J.Olufsen · Jan 5, 2012 · Viewed 9.6k times · Source

I need to iterate and delete all records of my datastore. I am using Google App engine Launcher to test it on local host. How to do it?

When I am trying to delete all recors in Person model that way:

 qObj = Person.all()
 db.delete(qObj)

I am getting error BadValueError: Property y must be a str or unicode instance, not a long I guess there is conflict in Model data types.

class Person(db.Model):
    name = db.StringProperty()
    x = db.StringProperty()
    y = db.StringProperty()
    group = db.StringProperty()

The field y = db.StringProperty() previously was y = db.IntegerProperty(). At this moment I need to flush all db records. How can I do that?

Is there is an opportunity to delete local file which stores all db records?

Answer

charlax picture charlax · Jan 5, 2012

The GQL language can only be used to retrieve entities or key (cf. http://code.google.com/appengine/docs/python/datastore/gqlreference.html)

You'll have to do this:

persons = Person.all()

for p in persons:
    p.delete()

Regarding the error BadValueError: Property y must be a str or unicode instance, not a long, you'll have to modify all the data (from integer to string) in the database to resolve the conflict.

It seems that you want to delete everything, so another solution would be to just go to the datastore administration page - http://localhost:8080/_ah/admin on localhost or via https://appengine.google.com/ - and remove everything.

You might find this useful: http://code.google.com/appengine/articles/update_schema.html