As mentioned by @gaurav-mantri, First it should be permitted on Blob Storage Account level policy/settings then can only be done via code. Also it is just not soft delete, it include creating version, snapshots of same blob & recover them or delete them. So need to look Documentation for whole detailed understanding.
You have to first enable soft delete for blob if not already done by configuration like:
// Retrieve a CloudBlobClient object and enable soft delete
CloudBlobClient blobClient = GetCloudBlobClient();
try
{
ServiceProperties serviceProperties = blobClient.GetServiceProperties();
serviceProperties.DeleteRetentionPolicy.Enabled = true;
serviceProperties.DeleteRetentionPolicy.RetentionDays = RetentionDays;
blobClient.SetServiceProperties(serviceProperties);
}
catch (StorageException ex)
{
Console.WriteLine("Error returned from the service: {0}", ex.Message);
throw;
}
Usage for soft delete:
CloudBlockBlob blockBlob = container.GetBlockBlobReference("HelloWorld");
//Create or update blob
await blockBlob.UploadFromFileAsync(ImageToUpload);
// Snapshot
await blockBlob.SnapshotAsync();
// Delete (including snapshots)
blockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
// Undelete
await blockBlob.UndeleteAsync();
// Recover
Console.WriteLine("\nCopy the most recent snapshot over the base blob:");
IEnumerable<IListBlobItem> allBlobVersions = container.ListBlobs(
prefix: blockBlob.Name, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Snapshots);
CloudBlockBlob copySource = allBlobVersions.First(version => ((CloudBlockBlob)version).IsSnapshot &&
((CloudBlockBlob)version).Name == blockBlob.Name) as CloudBlockBlob;
blockBlob.StartCopy(copySource);
For more details refer:
https://docs.microsoft.com/en-us/azure/storage/blobs/soft-delete-blob-overview
https://github.com/Azure-Samples/storage-dotnet-blob-soft-delete