views:

44

answers:

4

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)
+2  A: 

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.

Adam Crossland
I have about 100k entities I tried all at once, hit the too many for delete api error, so I put in a loop as below, what is the normal rate for deleting entities it seems ridiculously slow? still running
A: 
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)
A: 

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: http://stackoverflow.com/questions/108822/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: http://stackoverflow.com/questions/108822/delete-all-data-for-a-kind-in-google-app-engine/1023729#1023729

Cuga
I found the page to clear datastore as that will just be faster :)http://code.google.com/appengine/docs/python/tools/devserver.html
If it works for what you need, good. Just remember it will delete everything, not just a specific class, like is mentioned in the original post.
Cuga
+2  A: 

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)
Drew Sears