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:
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:
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 Checks like this with Blink:
Running these checks for unattached disks, managed or unmanaged, takes time and can be hard to incorporate into your team’s routine.
When you create a free Blink account, you can schedule these resource checks using pre-built automations. You set the frequency and start saving.
Get started and create your free Blink account today.