0
votes

Is it possible to programmatically delete Azure blob objects in bulks? Deleting objects one by one sometimes takes us several days.

We put a lot of new files on the Azure Blob Storage and all the outdated files we want deleted to avoid unwanted charges.

I've googled over the web/MSDN/Stack Overflow and found only one topic on MSDN from 2014 that was referring to create feature request on the Microsoft site.

3

3 Answers

6
votes

Is it possible to programmatically delete Azure blob objects in bulks?

No. You would need to delete blobs one-by-one.

What you could do to speed up the process is to create different containers (say a container for each year/month combination) and put related blobs in there. When you need to delete the blobs for that month/year, you simply delete that container.

1
votes

Of course you can (i.e. with Powershell)

$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzureStorageBlob -Container "container-name" -Context $Ctx -Blob TellMeWhatToDelete* | Remove-AzureStorageBlob

i.e: This deletes every Powerpoint Presentation in the documents container

Get-AzureStorageBlob -Container "documents" -Context $Ctx -Blob *.pptx | Remove-AzureStorageBlob

Or you can delete every docx file not changed in the last 30 days

Get-AzureStorageBlob -Container "documents" -Context $Ctx -Blob *.docx
| where {$_.LastModified -le (get-date).AddDays(-30) } | Remove-AzureStorageBlob
0
votes

u can delete blobs in batch. that will be one 1 call.

All you need is :

  1. List of URI's for the Blob.
  2. Connection to your storage account
  3. From package manager add using Azure.Storage.Blobs

in the return, the exception will give you details if there was any issue with deleting the blobs.

  BlobServiceClient service = new BlobServiceClient(connectionStringForStorageAccount);

            BlobBatchClient batchClient = service.GetBlobBatchClient();
            try
            {
                  //dont forget to add the include snapshots :)
               await batchClient.DeleteBlobsAsync(listofURIforBlobs,
    Azure.Storage.Blobs.Models.DeleteSnapshotsOption.IncludeSnapshots);

            }
            catch (Exception ex)
            {
                return (false, ex.Message);
            }