Reputation: 17173
What's the best practice for deleting an ndb entity that contains blob keys?
Should I wrap this in a try catch? Can I flatten the for loop into a batch (though ndb is asnyc)?
This is what I've tried so far but i'm not sure if it's the best way:
to_delete = BlogPost.query(ancestor=ndb.Key('BlogPost', int(id))).fetch(1)[0]
for blob_key in to_delete.blob_keys:
info = blobstore.BlobInfo(bob_key)
info.delete()
to_delete.key.delete()
delete_success = "successfully deleted"
self.response.out.write(delete_success)
Upvotes: 0
Views: 426
Reputation: 31928
I think that you should delete the blobs in a task instead of inline with the entity itself.
You can create a transaction that will queue the task and delete the entity at once.
Deleting the blobs in a task will give you the benefit of retries in case of an error and it will no delay the delete operation in case you have a lot of blobs.
Upvotes: 1