Honestly, Nothing! But that’s before reading this post. After reading this post, you don’t have to worry about a thing. In this post we will talk about Soft Delete functionality recently announced by Azure Storage. This super cool functionality will protect your blobs against accidental (or intentional) deletes. Please note that at the time of writing of this post, this functionality is in preview.
What is Soft Delete?
Until now when a blob is deleted from your storage account, it is permanently deleted. Unless you reach out to Azure Support in time, there’s no way to recover a deleted blob. Soft Delete functionality changes that. Think of this functionality as Recycle Bin/Trash on your local computer. When you delete a file (and not permanently delete it) from your computer, it actually goes to recycle bin/trash on your computer. You have an option of restoring the file from there. Soft Delete functionality works the same way (almost).
When Soft Delete is configured properly, as a blob gets deleted, it is not permanently deleted. It goes to a holding area (like recycle bin/trash) and stays there for “x” number of days (again configured by you). You will have an option of recovering the blob from there during those days. After that it will be automatically deleted (no need to do something like clear recycle bin/empty trash).
Sounds exciting! Let’s read on.
Configuring Soft Delete
By default soft delete feature is not enabled on a storage account. You will need to enable this feature and configure it. Configuration involves specifying the duration for which deleted blobs will remain in soft deleted status before being permanently deleted.
To work with this functionality, please ensure that you have the latest version of SDK (in case of .Net, version 9.0.0+).
To configure, you simply need to create an instance of DeleteRetentionPolicy and then call SetServiceProperties on the BlobClient. For example, if you want to retain the blobs for 10 days (maximum BTW is 365 days), this is what you would do:
static void SetDeleteRetentionPolicy(bool enabled, int retentionDays) { var cred = new StorageCredentials(accountName, accountKey); var account = new CloudStorageAccount(cred, true); var blobClient = account.CreateCloudBlobClient(); var deleteRetentionPolicy = new DeleteRetentionPolicy() { Enabled = enabled, RetentionDays = retentionDays }; var serviceProperties = new ServiceProperties() { Cors = null, DeleteRetentionPolicy = deleteRetentionPolicy, HourMetrics = null, MinuteMetrics = null, Logging = null }; blobClient.SetServiceProperties(serviceProperties); }
And this is how you would call this:
SetDeleteRetentionPolicy(true, 10);
It’s that simple! Really!!! And now your deleted blobs will remain in Soft Deleted state for 10 days from when they were deleted.
Listing Soft Deleted Blobs
Now that we’ve configured our storage account for soft deletes, let’s see how we can list the blobs that’re soft deleted.
Soft deleted blobs are returned as part of your regular blob listing provided you have included “Delete” option in the request. So your code to list blobs including deleted blobs would be something like this:
static List<CloudBlob> GetDeletedBlobs(string containerName) { var cred = new StorageCredentials(accountName, accountKey); var account = new CloudStorageAccount(cred, true); var blobClient = account.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); var blobs = container.ListBlobs(null, true, BlobListingDetails.Deleted).OfType<CloudBlob>().Where(b => b.IsDeleted).ToList(); foreach (var blob in blobs) { Console.WriteLine(blob.Name + '-' + blob.Properties.DeletedTime); } return blobs; }
Please note that you can’t simply fetch a list of deleted blobs. You can request storage service to include deleted blobs in the response along with other blobs. In other words, you will need to do filtering on the deleted status on the client side.
Currently we’re working on Blob management functionality in Cerulean, and there we chose to display both deleted and undeleted blobs in a single screen. This is how things look like there:
Undeleting Blobs
So now we have configured soft delete protection for our storage account. we can now list the deleted blobs. The next obvious step is how to undelete them (that’s the whole point, isn’t it ). Well its super simple actually. You just have to call “Undelete” on a deleted blob and the blob will be recovered. The code to undelete a blob would be something like this:
static void UndeleteBlob(CloudBlob blob) { blob.Undelete(); }
It’s that simple!!! Of course, we will be adding this functionality in Cerulean as well so that you don’t have to write code to do so .
A Few Other Things
A few things to keep in mind:
-
- Soft delete does not protect you against blob container deletes. If you have deleted a blob container by mistake or otherwise, essentially touch luck! You will not be able to recover blobs using this. If this happens and the blobs in that container are important to you, drop everything and reach out to Azure Support immediately. They may be able to help you out with this.
- Soft deletes are not supported for Premium LRS kind of accounts. In fact, you can’t even configure the soft delete settings there. However, please note that because this limitation is during preview phase only. You may be able to soft delete blobs in Premium LRS accounts when this functionality becomes generally available.
- When a soft delete blob is recovered, all the snapshots associated with that blob will be recovered.
- While the blob is in soft deleted state, even though you can list the blobs but any operation performed on that blob (except undelete of course) will result in a 404 (Not Found) error. And this makes sense considering the blob is technically deleted.
- If it has not been obvious to you so far, this feature is only available for blobs only .
If you uploaded a blob with the same name as the deleted blob one, you will lose the deleted blob forever. The deleted blob will be overwritten with the latest blob.Based on the feedback received from Azure Storage team, the previous statement is not correct. From what I am told, if you soft deleted a blob and then uploaded another blob with the same name but different type, then the blob will be lost forever. For example,
you uploaded a block blob, soft deleted it and then uploaded a page blob with the same name, then soft deleted block blob is lost.- Since a soft deleted blob is recoverable, you will pay for the storage costs associated with that blob (and its snapshots) until it is permanently deleted. So, please be careful in choosing the retention duration.
That’s it for this post! I hope you have found this information useful and I hope you will never have to ask this question “Oops! I deleted my blobs! What can I do?” .