Avkash Chauhan's Blog

Windows Azure, Windows 8, Cloud Computing, Big Data and Hadoop: All together at one place.. One problem, One solution at One time...

Programatically deleting older blobs in Windows Azure Storage

Programatically deleting older blobs in Windows Azure Storage

Rate This
  • Comments 2

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());
cloudBlob.DeleteIfExists(blobQuery);
}

Thats it!!

Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post
  • 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?

  • 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.  

    cloudBlob.FetchAttributes();

    if (cloudBlob.Properties.LastModifiedUtc < deleteFilesOlderThanThisDate)

       cloudBlob.DeleteIfExists();

Page 1 of 1 (2 items)