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.
Learn how to find and delete unattached Azure disks using the Azure CLI to optimize costs and improve security in your cloud environment.
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.
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.
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.
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.
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.
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:
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.
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.
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.
When this automation runs, it executes the following actions:
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.
Blink is secure, decentralized, and cloud-native. Get modern cloud and security operations today.