1
votes

I want to soft delete blob file inside my azure blob container from my DotNet Core application. I am using using "Azure.Storage.Blobs" nuget package. I manage to delete the blob file from azure blob container permanently, But I need to soft delete the file and not permanent delete. I gone through Microsoft documentation (https://azure.microsoft.com/en-us/blog/soft-delete-for-azure-storage-blobs-ga/) to do it with code and I don't get many. I have given the code I used to delete blob file. Please suggest me how can I soft delete the file from code.

        {
                BlobContainerClient container = new
     BlobContainerClient(blobConnectionString, containerName);
                //Delete Blob
                var response=container.DeleteBlob(blobNamewithFileExtension);
        }   
2

2 Answers

1
votes

But I need to soft delete the file and not permanent delete.

As such there's no method in Storage SDK (and REST API) to soft-delete a blob. There is only delete blob operation.

Soft delete is something that you set at the account level. Once soft delete is configured properly, whenever you delete a blob (using code similar to yours) it will be soft deleted instead of permanently deleted and will remain in the soft deleted state for a period based on the settings.

Please see this link regarding how you can configure blob soft delete settings for your storage account: https://docs.microsoft.com/en-us/azure/storage/blobs/soft-delete-blob-enable?tabs=azure-portal.

1
votes

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