Deleting entities from app engine
I'm going about deleting entries from my python app engine server like this:
try:
while True:
q = db.GqlQuery("SELECT __key__ FROM SampleData")
assert q.count()
db.delete(q.fetch(400))
开发者_StackOverflow中文版 time.sleep(0.2)
except Exception, e:
self.response.out.write(repr(e)+'\n')
pass
try:
while True:
q = db.GqlQuery("SELECT __key__ FROM UserData")
assert q.count()
db.delete(q.fetch(400))
time.sleep(0.2)
except Exception, e:
self.response.out.write(repr(e)+'\n')
pass
..but it just seems to ugly and I keep suspecting it's not entirely reliable. Is there some way to do this better to delete entries of some number of types instead of making one of each of these while loops?
Update: One restriction I have is that I am running this periodically via a cron job, so do not wish to be doing it manually (i.e. via the admin console for instance).
You can use the datastore admin tab of the admin console to delete all entities of a kind, see here for details:
http://code.google.com/appengine/docs/adminconsole/datastoreadmin.html#Deleting_Entities_in_Bulk
A few improvements:
- You don't need to sleep after each batch
- You should use the task queue, and be prepared to chain another task if you don't finish by the time the deadline hits.
- You should use cursors. If you don't, subsequent queries have to skip over all the 'tombstoned' rows you've already deleted in order to get to the still existing rows.
If you're deleting most or all of a kind, you may want to use the mapreduce library instead.
If you want to delete a lot of data, you might want to use the deferred library provided by Google -- have the cron job kick off a deferred task, which can delete your objects in batches:
class DeleteMapper(mapper.Mapper):
KIND = MyKindOfObject
# Delete all objects of type MyKindOfObject.
def map(self, key):
todelete = [key]
return ([], todelete)
You can likely do similar with the newer mapreduce library, however, I don't have an example for you.
精彩评论