How to delete all datastore in Google App Engine?

后端 未结 29 1888
夕颜
夕颜 2020-11-28 01:17

Does anyone know how to delete all datastore in Google App Engine?

29条回答
  •  心在旅途
    2020-11-28 01:54

    For any datastore that's on app engine, rather than local, you can use the new Datastore API. Here's a primer for how to get started.

    I wrote a script that deletes all non-built in entities. The API is changing pretty rapidly, so for reference, I cloned it at commit 990ab5c7f2063e8147bcc56ee222836fd3d6e15b

    from gcloud import datastore
    from gcloud.datastore import SCOPE
    from gcloud.datastore.connection import Connection
    from gcloud.datastore import query
    
    from oauth2client import client
    
    def get_connection():
      client_email = 'XXXXXXXX@developer.gserviceaccount.com'
      private_key_string = open('/path/to/yourfile.p12', 'rb').read()
    
      svc_account_credentials = client.SignedJwtAssertionCredentials(
        service_account_name=client_email,
        private_key=private_key_string,
        scope=SCOPE)
    
      return Connection(credentials=svc_account_credentials)
    
    
    def connect_to_dataset(dataset_id):
      connection = get_connection()
      datastore.set_default_connection(connection)
      datastore.set_default_dataset_id(dataset_id)
    
    if __name__ == "__main__":
      connect_to_dataset(DATASET_NAME)
      gae_entity_query = query.Query()
      gae_entity_query.keys_only()
      for entity in gae_entity_query.fetch():
        if entity.kind[0] != '_':
          print entity.kind
          entity.key.delete()
    

提交回复
热议问题