Clean up orphaned Azure resource manager disks

Firstly, let me apologise, this post isn’t about automatically cleaning up disks in Azure Resource Manager storage accounts, instead it’s about obtaining the information which you can use to understand which disks are orphaned and can or should be deleted.

When deleting a virtual machine on its own, I’m sure you’re aware that the disks are not automatically deleted as part of that process. The recommended best practice is to put every resource that shares the same lifecycle in to the same resource group which can then be deleted as a single entity, removing virtual machines, storage accounts, NICs, load balancers etc in one hit.

In some cases, there are occasions where it’s preferable to delete a single virtual machine on its own – doing this leaves orphaned disks behind in your storage accounts, gathering dust (and costing money!) unless you manually delete the disks yourself. We all know that as we stand up and delete VMs in Azure, there are occasions where cleaning up just isn’t convenient and the disks get left and forgotten about. Many moons later we decide it’s a good time to clean up those orphaned disks but finding them manually or using Storage Explorer is a painful process when there’s potentially hundreds of storage accounts in a subscription.

Now that Managed Disks have gone Generally Available, this problem will occur less and less for people using those but for now, most people would benefit from understanding their VHD/storage account layout and usage.

I wrote the following PowerShell script to iterate over all (or some of the) storage accounts in a subscription, pulling out the details of any “blob” in any container, ending with .vhd. Rather than trying to identify unleased blobs and automatically deleting them (which is very dangerous when you have Azure Site Recovery protected objects) I chose to simply generate an object containing the information of every blob (ending with .vhd). For those of us happy using Powershell for filtering, we can then filter the object as we see fit or just output to CSV and filter in Excel for example.

You can target this to all storage accounts, some (via a regex match or like condition) or a specific account by adjusting the Get-AzureRmStorageAccount  switches which act as the primary filtering mechanism. This script is non-destructive, it DOES NOT delete anything.

In my own tests, operating against ~90 storage accounts in a  single subscription, retrieving data on ~600 VHD objects, the information was populated in to the object and a CSV created in ~50 seconds. You can of course include additional information in the object by adjusting the object properties accordingly but I would highly recommend that you do not use any cmdlets inside the foreach loop, instead get the source information outside of the loop and then filter for that information from the object inside the foreach, like I do with $StorageAccount.

You can use the CSV to identify objects that are surplus to requirements, badly named, in the wrong account/type and act on the information as you desire, but don’t be tempted to auto-delete anything from its output unless you’re supremely confident you know what you’re doing and especially not if you use Azure Site Recovery to protect on-premises machines…doing so is a Bad Idea™

Before I disappear and as a small bonus for reading this far, in my quest to gather this information elegantly, and before I properly investigated the ICloudBlob object’s properties, I also put together the following function which, given an HTTPS endpoint and the storage account key, uses the Azure Storage API to get the properties of a blob as response headers from an HTTP request. If nothing else it shows how to interact with the Storage API via WebRequests in PowerShell and construct an authenticated request.

As a quick update, the above function can also be updated to easily become a replacement for Get-AzureStorageBlobContent  by changing the method to GET and updating the return object. Why would you do this? Well, in my scenario I might be using Azure Automation and don’t want to download a file to then open it and read its contents. Get-AzureStorageBlobContent  insists on downloading the file and I don’t want it to do that, the below function uses the Storage REST API to get a file’s contents as an object – obviously not something you’d do with a VHD but a CSV or JSON block blob for example isn’t large, is parseable and sits in an object nicely.


Leave a Reply

Your email address will not be published. Required fields are marked *