J.Olufsen
J.Olufsen

Reputation: 13915

How to delete a record using GQL?

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?

Upvotes: 8

Views: 10821

Answers (3)

MrCooL
MrCooL

Reputation: 926

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.

Upvotes: 0

charlax
charlax

Reputation: 26049

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

Upvotes: 9

pinerd314159
pinerd314159

Reputation: 544

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.

Upvotes: 1

Related Questions