How to delete all datastore in Google App Engine?

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

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

相关标签:
29条回答
  • 2020-11-28 01:35

    Here you go: Go to Datastore Admin, and then select the Entity type you want to delete and click Delete. Mapreduce will take care of deleting!

    0 讨论(0)
  • 2020-11-28 01:36

    You can clear the development server datastore when you run the server:

    /path/to/dev_appserver.py --clear_datastore=yes myapp

    You can also abbreviate --clear_datastore with -c.

    0 讨论(0)
  • 2020-11-28 01:40

    You can do it using the web interface. Login into your account, navigate with links on the left hand side. In Data Store management you have options to modify and delete data. Use respective options.

    0 讨论(0)
  • 2020-11-28 01:41
    for amodel in db.Model.__subclasses__():
                    dela=[]
                    print amodel
                    try:
                        m = amodel()
                        mq = m.all()
                        print mq.count()
                        for mw in mq:
                            dela.append(mw)
                        db.delete(dela)
                #~ print len(dela)
    
                    except:
                        pass
    
    0 讨论(0)
  • 2020-11-28 01:42

    Source

    I got this from http://code.google.com/appengine/articles/remote_api.html.

    Create the Interactive Console

    First, you need to define an interactive appenginge console. So, create a file called appengine_console.py and enter this:

    #!/usr/bin/python
    import code
    import getpass
    import sys
    
    # These are for my OSX installation. Change it to match your google_appengine paths. sys.path.append("/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine")
    sys.path.append("/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/yaml/lib")
    
    from google.appengine.ext.remote_api import remote_api_stub
    from google.appengine.ext import db
    
    def auth_func():
      return raw_input('Username:'), getpass.getpass('Password:')
    
    if len(sys.argv) < 2:
      print "Usage: %s app_id [host]" % (sys.argv[0],)
    app_id = sys.argv[1]
    if len(sys.argv) > 2:
      host = sys.argv[2]
    else:
      host = '%s.appspot.com' % app_id
    
    remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
    
    code.interact('App Engine interactive console for %s' % (app_id,), None, locals())
    



    Create the Mapper base class

    Once that's in place, create this Mapper class. I just created a new file called utils.py and threw this:

    class Mapper(object):
      # Subclasses should replace this with a model class (eg, model.Person).
      KIND = None
    
      # Subclasses can replace this with a list of (property, value) tuples to filter by.
      FILTERS = []
    
      def map(self, entity):
        """Updates a single entity.
    
        Implementers should return a tuple containing two iterables (to_update, to_delete).
        """
        return ([], [])
    
      def get_query(self):
        """Returns a query over the specified kind, with any appropriate filters applied."""
        q = self.KIND.all()
        for prop, value in self.FILTERS:
          q.filter("%s =" % prop, value)
        q.order("__key__")
        return q
    
      def run(self, batch_size=100):
        """Executes the map procedure over all matching entities."""
        q = self.get_query()
        entities = q.fetch(batch_size)
        while entities:
          to_put = []
          to_delete = []
          for entity in entities:
            map_updates, map_deletes = self.map(entity)
            to_put.extend(map_updates)
            to_delete.extend(map_deletes)
          if to_put:
            db.put(to_put)
          if to_delete:
            db.delete(to_delete)
          q = self.get_query()
          q.filter("__key__ >", entities[-1].key())
          entities = q.fetch(batch_size)
    

    Mapper is supposed to be just an abstract class that allows you to iterate over every entity of a given kind, be it to extract their data, or to modify them and store the updated entities back to the datastore.

    Run with it!

    Now, start your appengine interactive console:

    $python appengine_console.py <app_id_here>
    

    That should start the interactive console. In it create a subclass of Model:

    from utils import Mapper
    # import your model class here 
    class MyModelDeleter(Mapper):
        KIND = <model_name_here>
    
        def map(self, entity):
            return ([], [entity])
    

    And, finally, run it (from you interactive console): mapper = MyModelDeleter() mapper.run()

    That's it!

    0 讨论(0)
  • 2020-11-28 01:43

    If you have a significant amount of data, you need to use a script to delete it. You can use remote_api to clear the datastore from the client side in a straightforward manner, though.

    0 讨论(0)
提交回复
热议问题