Search code examples
javagoogle-app-enginegoogle-cloud-datastoreblobstore

Deleting orphan blobs in appengine blobstore


I deleted a large number of objects from the datastore that I din't require (around 7000 of them). Each of those objects had a blob associated with it, referenced by a String (blob key).

As you might have guessed by now, I forgot to delete those blobs. Now I don't have any reference to them, but I want to delete them. I can't seem to find a way to do that. Any help will be appreciated.

UPDATE Found the solution.

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
List<BlobInfo> blobsToCheck = new LinkedList<BlobInfo>(); 
Iterator<BlobInfo> iterator = null;
if(afterBlobKey == null){
    iterator = new BlobInfoFactory().queryBlobInfos();
}else{
    iterator = new BlobInfoFactory().queryBlobInfosAfter(new BlobKey(afterBlobKey));
}

while(iterator.hasNext()){

    blobsToCheck.add(iterator.next());

}

//Check those blobs if they have reference in datastore
//Delete using blobstoreService.delete(blobKey);

Solution

  • UPDATE Found the solution, BlobInfoFactory().queryBlobInfos() is what I was looking for.

        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        List<BlobInfo> blobsToCheck = new LinkedList<BlobInfo>(); 
        Iterator<BlobInfo> iterator = null;
        if(afterBlobKey == null){
            iterator = new BlobInfoFactory().queryBlobInfos();
        }else{
            iterator = new BlobInfoFactory().queryBlobInfosAfter(new BlobKey(afterBlobKey));
        }
    
        while(iterator.hasNext()){
    
            blobsToCheck.add(iterator.next());
    
        }
    
        //Check those blobs if they have reference in datastore
        //Delete using blobstoreService.delete(blobKey);