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!!