8

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?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
  • When you do qObj = Person.all(), you're setting qObj to a list of Person objects. As far as I know, you can't call delete on that list - you have to iterate through the list and delete each object separately. – pinerd314159 Jan 05 '12 at 03:25
  • http://code.google.com/appengine/docs/python/datastore/functions.html#create_config ... delete(models) Deletes one or more model instances from the datastore. Arguments: models A model instance, a Key for an entity, or a list (or other iterable) of model instances or keys of entities to delete. – J.Olufsen Jan 05 '12 at 03:39
  • I just tried [`db.delete( Person.all() )`](https://developers.google.com/appengine/docs/python/datastore/functions#delete), using your exact `Person` model, and it works perfectly (clears all `Person` objects from db) – bobobobo Apr 10 '13 at 00:58

3 Answers3

9

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

charlax
  • 25,125
  • 19
  • 60
  • 71
  • This is __not correct__. You don't need the explicit `for`. This was either fixed or something else was wrong – bobobobo Apr 10 '13 at 01:03
1

If you have a variable that stores a record on the database then you can simply use delete().

That is, say you have an Entity called Persons, you can do:

personToDelete = db.GqlQuery("SELECT * FROM Persons WHERE name='Joe'");
person = personToDelete[0];
person.delete();

You do also have to import the database library, but I'm assuming you do that anyway given that you're clearly using the database.

pinerd314159
  • 534
  • 4
  • 10
0

Just to share a helpful tips on the already accepted answer.

You can do the following with db.delete:

persons = Person.all()
d = []
for p in persons:
    d.append(p)

db.delete(d)

This saves a lot of db operations.

MrCooL
  • 926
  • 3
  • 15
  • 30