Forum Discussion
Fromelard
Dec 16, 2024Steel Contributor
Powershell Script to remove all Blobs from Storage account
With large number of Blobs in Storage Account, the manual cleanup from the Portal is more complicated and time consuming, as it's per set of 10'000.
This script is simple and and can be executed in background to clean all items from a defined Blob Container. You have to specify the Storage Account connection string and the blob container name.
[string]$myConnectionString = "DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourKeyFromStorageAccountConnectionString;EndpointSuffix=core.windows.net"
[string]$ContainerName = "YourBlobContainerName"
[int]$blobCountAfter = 0
[int]$blobCountBefore = 0
$context = New-AzStorageContext -ConnectionString $myConnectionString
$blobCountBefore = (Get-AzStorageBlob -Container $ContainerName -Context $context).Count
Write-Host "Total number of blobs in the container Before deletion: $blobCount" -ForegroundColor Yellow
Get-AzStorageBlob -Container $ContainerName -Context $context | ForEach-Object {
$_ | Remove-AzureStorageBlob # or: Remove-AzureStorageBlob -ICloudBlob $_.ICloudBlob -Context $ctx
}
$blobCountAfter = (Get-AzStorageBlob -Container $ContainerName -Context $context).Count
Write-Host "Total number of blobs in the container After deletion : $blobCount" -ForegroundColor Green
It was used for large blob storage container with more than 5 millions of blob items.
Sources:
https://stackoverflow.com/questions/57119087/powershell-remove-all-blobs-in-a-container
Fab
- FromelardSteel Contributor
Script updated with the deletion command:
[string]$myConnectionString = "DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourKeyFromStorageAccountConnectionString;EndpointSuffix=core.windows.net" [string]$ContainerName = "YourBlobContainerName" [int]$blobCountAfter = 0 [int]$blobCountBefore = 0 $context = New-AzStorageContext -ConnectionString $myConnectionString $blobCountBefore = (Get-AzStorageBlob -Container $ContainerName -Context $context).Count Write-Host "Total number of blobs in the container Before deletion: $blobCount" -ForegroundColor Yellow Get-AzStorageBlob -Container $ContainerName -Context $context | ForEach-Object { $_ | Remove-AzStorageBlob -ICloudBlob $_.ICloudBlob -Context $context } $blobCountAfter = (Get-AzStorageBlob -Container $ContainerName -Context $context).Count Write-Host "Total number of blobs in the container After deletion : $blobCount" -ForegroundColor Green
It's also possible to add the batch set using the maxcount
Get-AzStorageBlob -Container $ContainerName -Context $context -MaxCount 10000
Fab