Created
January 23, 2015 15:48
-
-
Save theacodes/f7406d6f4f99c8950c8b to your computer and use it in GitHub Desktop.
Simple search and delete task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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