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());
cloudBlob.DeleteIfExists(blobQuery);
}
Thats it!!
Comments
Anonymous
May 24, 2012
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?Anonymous
July 02, 2012
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();Anonymous
February 19, 2015
Today (Feb 2015) this code is obsolete :/Anonymous
April 30, 2015
Yes, the code is obsolete. I ended up using container.ListBlobs and then checking the dates.