Skip to content

Instantly share code, notes, and snippets.

@theacodes
Created January 23, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theacodes/f7406d6f4f99c8950c8b to your computer and use it in GitHub Desktop.
Save theacodes/f7406d6f4f99c8950c8b to your computer and use it in GitHub Desktop.
Simple search and delete task
from google.appengine.ext import ndb
def reaping_task():
search_string = "spam"
# Disable all caching, this prevents out of memory errors as we
# go through the whole dataset. If we don't do this, ndb will
# try to keep a copy of every entity we look at.
ndb.get_context().set_cache_policy(lambda key: False)
# Replace MyModel with your actual ndb Model class.
q = MyModel.query()
cursor = None
while True:
results, cursor, more = q.fetch_page(50, start_cursor=cursor)
for item in results:
# Replace content with the actual field that you want to search.
if search_string in item.content:
item.key.delete()
if not cursor or not more:
break
# To start this, run:
from google.appengine.ext import deferred
deferred.defer(reaping_task)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment