Snapshots provide read-only versions of blobs. Once a snapshot has been created, it can be read, copied, or deleted, but not modified. http://msdn.microsoft.com/en-us/library/ee691971.aspx
Windows Azure Platform Training Kit (dated: 05/18/2011), Lab: Exploring Windows Azure Storage VS2010 - Exercise 2: Working with Blobs - This lab specifically says “To delete a blob that contains snapshots, all of its snapshots must be deleted first. (That functionality is not provided in this solution)“.It took me a while to determine how to format the SnapshotTime and formulate the request to delete a specific Snapshot. It is much easier if you delete a blob and want to delete all associated Snapshots - I wanted to delete a single snapshot - so I have created this brief writeup.
If you stumbled upon this writeup and are only interested in the format. Here it is. Note that it must be UrlEncoded in your Blob URI.
yyyy-MM-ddTHH:mm:ss.fffffffZ
Step 1 - Storage:
Setup Storage Account, Blob Client and Blob Container references:
// DataConnectionString and ContainerName are role configuration values
CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting(”DataConnectionString”);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue(”ContainerName”));
Step 2 - Retrieve Snapshot data
This will be done independently based on your application. You should have some mechanism to store and track the SnapshotTime values so they can be passed through to the code that deletes the Snapshot. Here is an example of very basic Snapshot retrieval:
// retrieve snapshots
IEnumerable<IListBlobItem> snapshotList = container.ListBlobs(new BlobRequestOptions()
{
UseFlatBlobListing = true,
BlobListingDetails = BlobListingDetails.Snapshots
});
Step 3 - Delete Blob Snapshot
// snapshotTime should be formatted as “yyyy-MM-ddTHH:mm:ss.fffffffZ”
string snapshotTime = [...]
// select specific snapshot
CloudBlob snapshotBlob = (from CloudBlob x in snapshotList
where x.SnapshotTime.HasValue &&
x.SnapshotTime.Value.ToString(”yyyy-MM-ddTHH:mm:ss.fffffffZ”) == snapshotTime
select x).SingleOrDefault();
// if blob matched
if (snapshotBlob != null)
{
// URL encode time
string encodedTime = Server.UrlEncode(snapshotTime);
string snapshotUri = string.Format(”{0}?snapshot={1}”, snapshotBlob.Uri, encodedTime);
CloudBlob snapshotReference = new CloudBlob(snapshotUri, client);
snapshotReference.DeleteIfExists();
}