user291071
user291071

Reputation: 351

how to delete an entire db.model class in google app engine easily?

Hi I read a few post on this topic, lets say I want to delete every object for a given class db.model such as LinkRating2, is there a way to delete it on startup with a simple command? I thought i remember seeing this somewhere eg --clear datastore?, otherwise I have been trying various methods in sdk console but all seem to be memory crashing as the file is taking forever just to load and start I don't think there is any memory left about lol. So what would be best code method to delete entire entity?

class LinkRating2(db.Model):
    user = db.StringProperty()
    link = db.StringProperty()
    rating2 = db.FloatProperty()

tried this but this is super slow

results2 = LinkRating2.all()
results = results2.fetch(500)

while results:
    db.delete(results)
    results = results2.fetch(500)

Upvotes: 2

Views: 511

Answers (4)

Drew Sears
Drew Sears

Reputation: 12838

is there a way to delete it on startup with a simple command? I thought i remember seeing this somewhere eg --clear datastore?

If you're talking about the development server, yes. dev_appserver.py --clear_datastore myapp will start you up with a fresh datastore. This is the best option when you're working locally.

I didn't bother putting it in a while loop, as trying to delete all of the entities in one go probably stands a good chance of exceeding the 30-second deadline. You'll just need to do this until all of the entities are gone.

If you do want to wipe out every entity of a given type in production, this is a good excuse to use remote_api instead of writing a web-based handler, both to circumvent the 30 second deadline and to reduce the chance of your entitypocalypse code being run again by accident.

One last thing: if you want to delete some entities but you don't want to bother loading the model definition, you can use the low level datastore API:

from google.appengine.api import datastore

kind = 'LinkRating2'
batch_size = 1000

query = datastore.Query(kind=kind, keys_only=True)
results = query.Get(batch_size)
while results:
  print "Deleting %d %s entities" % (batch_size, kind)
  datastore.Delete(results)
  results = query.Get(batch_size)

Upvotes: 2

Cuga
Cuga

Reputation: 17904

The --clear-datastore flag will clear all the data from the database-- not just of a specific class as it seems you want.

This question has already been asked: Delete all data for a kind in Google App Engine

I think this answer will provide you with what you want. It deletes items by their keys, which should be much faster: Delete all data for a kind in Google App Engine

Upvotes: 0

user291071
user291071

Reputation: 351

results = db.Query(Final, keys_only=True).fetch(1000)
while len(results) > 0:
    db.delete(results)
    results = db.Query(Final, keys_only=True).fetch(1000)

Upvotes: 0

Adam Crossland
Adam Crossland

Reputation: 14213

You are much better off working simply with keys than with entire entities. Querying just for entity keys is much faster than fetching entire entities, and you only need the keys for delete.

results = db.Query(LinkRating2, keys_only=True).fetch(1000)
if len(results) > 0:
    db.delete(results)

I didn't bother putting it in a while loop, as trying to delete all of the entities in one go probably stands a good chance of exceeding the 30-second deadline. You'll just need to do this until all of the entities are gone.

Upvotes: 2

Related Questions