Search code examples
google-app-engineblobstore

how to delete orphaned blob in google app engine


I have been following instructions on this site - Is it possible to find and delete orphaned blobs in the app engine blobstore? - on how to delete orphaned blobs, but I keep getting an error.

from google.appengine.ext.blobstore import BlobInfo

#Here is where I store my blobKeys
class Content(ndb.Model):
  blobKey = ndb.BlobKeyProperty(required=False)

#Here is what I've been trying to follow
class Refresh(blobstore_handlers.BlobstoreUploadHandler):
  def get(self):
  blobs = BlobInfo.all().fetch(500)
  for blob in blobs:
    if not Content.all().filter("blob_ref =", blob.key()).count(1): #ERROR
      blob.delete()

I keep getting the error AttributeError: type object 'Content' has no attribute 'all'

The poster mentions that "If your BlobReferenceProperty field is indexed, then yes, it's quite possible." Could it be an issue that I'm using ndb.BlobKeyProperty rather than BlobReferenceProperty? Thanks, for reading.

Update Refer to Expected BlobKey but instead I get a BlobInfo object - How to get BlobKey from BlobInfo object? to see the solution


Solution

  • You are confusing db and ndb apis

    To query with ndb you use the query method

    In your case

    blobs = BlobInfo.query().fetch(500)

    But this is not really correct. You should rewrite it to

    for blob in BlobInfo.query(options=QueryOptions(batchsize=200))
       # do stuff