The Ops Community

Cover image for Finding and Deleting Unattached Disks with the Azure CLI
Patrick Londa for Blink Ops

Posted on • Updated on

Finding and Deleting Unattached Disks with the Azure CLI

In Azure, deleting a virtual machine (VM) does not delete the disks attached to it. This way, if you accidentally delete a VM, you won't lose all of your data. However, you will continue to be charged for any disks that remain, including unattached disks.

To avoid unnecessary fees, it's important to routinely clean up any unattached disks. While this is possible through the portal, finding, reviewing, and deleting a large number of unattached disks through the browser interface can be unwieldy.

Here's how to use the Azure command-line interface (CLI) to find, review, and delete both managed and unmanaged unattached disks.

What Are Managed Disks vs. Unmanaged Disks?

Microsoft offers two types of storage disks — managed and unmanaged. For unmanaged disks, you must create a storage account that will hold the disks for your VMs.

With managed disks, Azure creates the storage resources for you. You just specify the disk size and type, and then provision the disk. Managed disks are simpler to set up, more secure, and more resilient than unmanaged disks. However, unmanaged disks are cheaper.

Using the Azure CLI Tool to Find and Delete Managed Unattached Disks

To check for and delete unattached managed disks using the Azure CLI, use the following script:

deleteUnattachedDisks=0
unattachedDiskIds=$(az disk list --query '[?managedBy==`null`].[id]' -o tsv)
for id in ${unattachedDiskIds[@]}
do
   if (( $deleteUnattachedDisks == 1 ))
   then
       echo "Deleting unattached Managed Disk with Id: "$id
       az disk delete --ids $id --yes
       echo "Deleted unattached Managed Disk with Id: "$id
   else
       echo $id
   fi
done
Enter fullscreen mode Exit fullscreen mode

This is a two-step process. First, you'll check for all unattached managed disks. Then, after reviewing the list of unattached managed disks and determining that they're suitable for deletion, you'll delete them.

1. Check for Unattached Managed Disks

The first time you run the script, keep "deleteUnattachedDisks" set to 0. This will output all of your unattached managed disks, so you can review them before deletion.

2. Delete All Unattached Managed Disks

Once you've reviewed the list of unattached managed disks and determined that you want to delete them, run the script again. This time, however, set "deleteUnattachedDisks" to 1. When you run the script with "deleteUnattachedDisks" set to 1, it will delete all unattached managed disks.

Using the Azure CLI Tool To Find and Delete Unmanaged Unattached Disks

To check for and delete unattached, unmanaged disks using the Azure CLI, use the following script:

deleteUnattachedVHDs=0
storageAccountIds=$(az storage account list --query [].[id] -o tsv)
for id in ${storageAccountIds[@]}
do
   connectionString=$(az storage account show-connection-string --ids $id --query connectionString -o tsv)
   containers=$(az storage container list --connection-string $connectionString --query [].[name] -o tsv)
   for container in ${containers[@]}
   do 

       blobs=$(az storage blob list --show-next-marker -c $container --connection-string $connectionString --query "[?properties.blobType=='PageBlob' && ends_with(name,'.vhd')].[name]" -o tsv)

       for blob in ${blobs[@]}
       do
           leaseStatus=$(az storage blob show -n $blob -c $container --connection-string $connectionString --query "properties.lease.status" -o tsv)

           if [ "$leaseStatus" == "unlocked" ]
           then
               if (( $deleteUnattachedVHDs == 1 ))
               then
                   echo "Deleting VHD: "$blob" in container: "$container" in storage account: "$id
                   az storage blob delete --delete-snapshots include  -n $blob -c $container --connection-string $connectionString
                   echo "Deleted VHD: "$blob" in container: "$container" in storage account: "$id

                else
                   echo "StorageAccountId: "$id" container: "$container" VHD: "$blob
               fi
           fi
       done
   done
done
Enter fullscreen mode Exit fullscreen mode

The process for deleting unattached Azure disks that are unmanaged is very similar to the process for deleting unattached managed disks:

1. Check for Unattached Unmanaged Disks

The first time you run the script, keep "deleteUnattachedDisks" set to 0. This will output all of your unattached unmanaged disks, so you can review them before deletion.

2. Delete All Unattached Unmanaged Disks

Once you've reviewed the list of unattached unmanaged disks and determined that you want to delete them, run the script again. This time, however, set "deleteUnattachedDisks" to 1. When you run the script with "deleteUnattachedDisks" set to 1, it will delete all unattached unmanaged disks.

Discussion (0)