Delete Unattached Azure Storage Accounts

While creating a VM on Azure we needed to setup the storage account to store the VHD in a page blob. With the VM Creation Wizard the user created storage accounts and the default names given to it are often confusing to assign it with a VM. Microsoft introduced the concept of Managing Disk last year which gives you the capability of hosting this VHDs for us without taking the overhead of managing this storage accounts.

After deleting the VMs,the users are not sure whether they have deleted the associated storage account or not? People often use same storage account for multiple VHDs as well so they’re usually not sure whether is there is any vhd in the storage which is still connected to VM or not. Will it be safe to delete the storage or not? This is always a big question in the users mind. Moreover, if the disk is not attached to the VM still the user need to pay the price for the storage in their billing. In order to solve this issue, I have created the following script.

This Script Loops over all the storage account in your subscription and search for the page blob with extension of .VHD and check for the lease. If the lease is released, that means it might be safe to delete the Storage. The user need to authenticate himself in the first step to run this script.

$toDeleteUnattachedVHDs = $false

$storageAccounts = Get-AzureRmStorageAccount

foreach ($storageAccount in $storageAccounts) {
$Key = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName)[0].Value

$context = New-AzureStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $Key
$containers = Get-AzureStorageContainer -Context $context

foreach ($container in $containers) {

$blobs = Get-AzureStorageBlob -Container $container.Name -Context $context

#Fetch all the Page blobs with extension .vhd

$blobs | Where-Object {$_.BlobType -eq 'PageBlob' -and $_.Name.EndsWith('.vhd')} | ForEach-Object {

#If a Page blob is  attached as disk then LeaseStatus will not be unlocked

if ($_.ICloudBlob.Properties.LeaseStatus -eq 'Unlocked') {

if ($toDeleteUnattachedVHDs -eq $true) {

Write-Host "Deleting unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"

$_ | Remove-AzureStorageBlob -Force

Write-Host "Deleted unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"


else {








I have kept toDeleteUnattachedVHDs = $false as it will delete only if its true. With the default values, script will show you the list of URIs of the VHDs for you to evaluate whether you want to delete it or not. If you change the value of this variable to $true then it will delete all the storage where the VHDs are not attached to a VM.

Please note that it’s a irreversival action, so I advice the users to make sure that they’re keeping the value of toDeleteUnattachedVHDs = $true only if they’re sure about the deletion. In the first run it’s always safer to run with the value as $false and once you verified then convert it to $true.



Leave a Reply

Your email address will not be published.