Finding and Deleting Unattached Disks with the Azure CLI

Learn how to find and delete unattached Azure disks using the Azure CLI to optimize costs and improve security in your cloud environment.

Patrick Londa
Author
Feb 21, 2022
 • 
 min read
Share this post

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.

Blink Automation: Ensure Unattached Azure Disks are Removed
Blink + Azure
Try This Automation

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

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

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.

Automate Detecting Unattached Azure Disks with Blink:

Running these checks for unattached disks, managed or unmanaged, takes time and can be hard to incorporate into your team’s routine.

With Blink, you can run this automation to scan your Azure account for unattached disks and send a report to a designated email address.

Blink Automation: Ensure Unused Disks are Removed in Azure
Blink Automation: Ensure Unused Disks are Removed in Azure

When this automation runs, it executes the following actions:

  1. Checks for unattached disks in your Azure account.
  2. Sends a report with the results via email.

You can import this automation from the Blink library and customize it however you like. For example, you could send a weekly Slack notification with a report on unattached disks, with the ability to approve their deletion via Slack.

In Blink, you can also create automations from scratch to meet your team’s unique needs using the hundreds of drag-and-drop actions available from a wide range of tools.

Get started with Blink today to see how easy automation can be.

Expert Tip