Programmatically delete Azure blob storage objects

2019-09-16 02:11发布

问题:

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.

回答1:

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.



回答2:

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