Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Created April 14, 2011 21:33
Show Gist options
  • Save mccutchen/920610 to your computer and use it in GitHub Desktop.
Save mccutchen/920610 to your computer and use it in GitHub Desktop.
Map-reduce task to clean up App Engine datastore entities whose models have changed
from mapreduce import operation as op
def scrubber(entity):
"""Deletes any data from the underlying datastore that are no longer
present in the entity's model definition.
"""
# What properties are still defined on the model?
valid_props = set(entity.properties().keys())
# What properties are attached in the datastore?
raw_props = set(entity._entity.keys())
# We need to delete those that are in raw_props but not valid_props
to_delete = raw_props - valid_props
if to_delete:
for field in to_delete:
del entity._entity[field]
yield op.db.Put(entity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment