How to delete all the entries from google datastore?

前端 未结 1 486
無奈伤痛
無奈伤痛 2020-11-30 16:16

I created a page to delete all entries from datastore. I am using self.key.delete() for this but it has stopped working (it worked once but it isn\'t working an

相关标签:
1条回答
  • 2020-11-30 16:19

    Assuming that:

    • you're using the ndb library
    • you have a models.py file with the entity models

    Then you can try something like this, hooked up in one of the app's handler:

    from google.appengine.ext import ndb
    import inspect
    import models
    
    for kind, model in inspect.getmembers(models):
        if not isinstance(model, ndb.model.MetaModel):
            continue
        cursor = None
        while True:
            keys, next_cursor, more = \
                model.query().fetch_page(500, keys_only=True, start_cursor=cursor)
            if keys:
                ndb.delete_multi_async(keys)
            if more and next_cursor:
                cursor = next_cursor
            else:
                break
    

    If you have a lot of entities the above may be killed after a while with DeadlineExceededError (after it should have deleted a bunch of entities). Either you repeat the request until they're all gone.

    Or maybe even try splitting the work on deferred tasks, staggered in time to not have too many simultaneous requests which could cause instance explosion. Something like this:

    from google.appengine.ext import deferred
    from google.appengine.ext import ndb
    import inspect
    import models
    
    def delete_keys(keys):
        ndb.delete_multi(keys)
    
    delay = 0
    for kind, model in inspect.getmembers(models):
        if not isinstance(model, ndb.model.MetaModel):            
            continue
        cursor = None
        while True:
            keys, next_cursor, more = \
                model.query().fetch_page(500, keys_only=True, start_cursor=cursor)
            if keys:
                deferred.defer(delete_keys, keys, _countdown=delay)
                delay += 5
            if more and next_cursor:
                cursor = next_cursor
            else:
                break
    
    0 讨论(0)
提交回复
热议问题