Programatically deleting older blobs in Windows Azure Storage

When you use Windows Azure Storage to dump your diagnostics data, it may possible that you have lots of blobs which are old and you dont have any use of those blobs. You can delete them manually however you know how much it will take so to expedite the older blob deletion you can write some code which will do it automatically for you depend on your blob selection criteria.

To make your job simple, the best thing is that each blob has a property which indicate when the particular blob was last modified. This value can tell you how old this blob is. And using this value you can select older blobs and delete them.

Most Importantly "There is no way to do batch operations on blobs" so you will have to delete them one by one or you can run the code in loop to select all the blobs and delete them.

For example you can choose to delete blobs which are older then a week or two weeks or a month. So you can choose what blob selection criteria you will use to select the blobs and then delete them.

CloudBlobClient blobClient = connectionManager.GetBlobServiceClient(storageUri);
// Next lets select then container which you are looking for your old blobs.
CloudBlobContainer container = blobClient.GetContainerReference(this.WadBlobContainerName);

// Now we will select blob query object which will give us the list of selected filtered blobs
BlobRequestOptions blobQuery = new BlobRequestOptions();
blobQuery.BlobListingDetails = BlobListingDetails.None;
blobQuery.UseFlatBlobListing = true;

// Now we will setup Blob access condition option which will filter all the blobs which are not modified for X (this.DaysToKeep) amount of days
blobQuery.AccessCondition = AccessCondition.IfNotModifiedSince(DateTime.UtcNow.AddDays(-1 * this.DaysToKeep));

// Lets Query to do its job
IEnumerable<IListBlobItem> blobs = container.ListBlobs(blobQuery);

foreach (IListBlobItem blob in blobs)
CloudBlob cloudBlob = container.GetBlobReference(blob.Uri.ToString());

Thats it!!

Comments (4)
  1. Dan says:

    I found your post via google, and it looked like it was perfect for my purposes, except when it runs, it doesn't seem to apply the IfNotModifiedSince condition correctly.  In the foreach loop, the cloudBlob has an LastModifiedUTC property of 1/1/0001, so it doesn't seem that this works.  Any ideas?

  2. Keith says:

    Dan: Try adding "cloudBlob.FetchAttributes();" to get the LastModifiedUTC property.  

    Avkash: When I tested this, ListBlobs(blobQuery) always returned the same list of blobs, no matter how I modified the AccessCondition.  I'm not sure why.  I ended up writing this, but I don't like it.  


    if (cloudBlob.Properties.LastModifiedUtc < deleteFilesOlderThanThisDate)


  3. G says:

    Today (Feb 2015) this code is obsolete :/

  4. David says:

    Yes, the code is obsolete. I ended up using container.ListBlobs and then checking the dates.

Comments are closed.

Skip to main content