在Google App Engine中删除某种类型的所有数据

47 投票
19 回答
29243 浏览
提问于 2025-04-11 09:18

我想在Google App Engine上删除某种特定类型的所有数据。有什么好的方法吗?我写了一个删除的脚本(算是个小窍门),但是因为数据量太大,几百条记录后就超时了。

19 个回答

22

现在你可以使用Datastore管理工具来完成这个操作:https://developers.google.com/appengine/docs/adminconsole/datastoreadmin#Deleting_Entities_in_Bulk

28

我现在是通过它们的键来删除这些实体,感觉这样更快。

from google.appengine.ext import db

class bulkdelete(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        try:
            while True:
                q = db.GqlQuery("SELECT __key__ FROM MyModel")
                assert q.count()
                db.delete(q.fetch(200))
                time.sleep(0.5)
        except Exception, e:
            self.response.out.write(repr(e)+'\n')
            pass

我从终端运行 curl -N http://...

6

谷歌的官方回答是,你需要分批删除数据,每次处理一部分,而不是一次性删除所有。你可以使用AJAX、meta refresh,或者通过脚本不断请求你的网址,直到所有的数据都被删除完。

撰写回答