Skip to content

Instantly share code, notes, and snippets.

@crizCraig
Last active February 10, 2020 06:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crizCraig/2816250 to your computer and use it in GitHub Desktop.
Save crizCraig/2816250 to your computer and use it in GitHub Desktop.
Deleting all entities in a namespace on Google App Engine with Python.
DEV = os.environ['SERVER_SOFTWARE'].startswith('Dev')
APP_ID = os.environ['APPLICATION_ID']
def deleteDBandMemcache():
if not DEV and APP_ID.find('test') == -1:
# We're not in Kansas anymore. i.e. This is PRODUCTION.
# To test in the cloud, the app id must contain 'test'.
logging.error('trying to run tests on production server = no beuno')
return
"""Delete all entities in a namespace."""
from google.appengine.api import namespace_manager
######################################
""" ####### IMPORTANT ########## """
TESTSPACE = 'test'
namespace_manager.set_namespace(TESTSPACE)
""" ##### DO NOT DELETE ######## """
######################################
if namespace_manager.get_namespace() != 'test':
logging.error('Tyring to delete from namespace other than test = muy mal')
return
# Clear datastore entities
for model in list(get_all_models()):
ndb.delete_multi(model.query().fetch(999999, keys_only=True))
# Clear memcache
ndb.get_context().clear_cache()
def get_all_models():
import sys
import inspect
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj):
yield obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment