BlueXP Blog

Find and Delete Unused Azure Disks to Reduce Azure Storage Costs

Written by Yifat Perry, Technical Content Manager | Nov 4, 2020 1:44:00 PM

One of the major reasons your Azure storage costs may be higher than you expect is the presence of unused virtual hard disks (VHDs) in your Azure subscription. A key Azure cost management best practice is to find and delete all of these unused resources.  

Unused disks can accumulate after migration projects, diagnostic drives of VMs, or backups, but most unused disks are orphaned disks. When VMs are deleted, the disks are not deleted automatically, which leaves behind what are known as orphaned disks. These orphaned disks take up space and incur charges that most Azure users don’t even know they’re paying.

In this post we’ll show you how to delete unused managed and unmanaged Azure disks using either the Azure portal or through PowerShell so you can lower your Azure storage costs.

To get started now: 

How Unused Disks Impact Azure Storage Costs

There are two types of disks—managed disks and unmanaged disks. In the case of unmanaged disks, the underlying storage account is also managed by the user. For standard unmanaged disks, customers are charged for the size of the data in the disk. Premium unmanaged disks, on the other hand, are charged based on the SKU being used, irrespective of the size of data in the disks. You will also be charged for storage transactions in case of standard unmanaged disks.  

Managed disks, as the name indicates, are managed by Azure platform where the underlying storage is abstracted from the user. Unmanaged disk charges are incurred based on the selected SKU. When an unmanaged disk is orphaned, storage transaction costs will not grow. However, the disk  costs will continue to show up in your invoice for both managed and unmanaged disks. This can turn out to be expensive, especially if the disk  type is Premium or Ultra disk, as both of these disk types are  charged based on their provisioned size. That means if you provision a 127 GB premium disk but only use 50 GB of it, you will be charged for 127 GB and the price will reflect the closest premium storage SKU, which is P10 (128 GB). 

Let’s envision the impact of unused disks with an example: Company XYZ had a VM hosting their Web App services. This VM hosted their mission-critical app services and had two premium disks (one P10 and one P20) attached to it along with a GRS Standard disk (127 GB).

Six months ago, XYZ migrated their app services to the Azure PaaS offering, Azure Web App Services, but had not deleted the disks associated with the VM. The average cost of XYZ's consumed resources for these six months was $1,500 and the unused disks accounted for ~7% of the overall cost.

How to Avoid Creating Orphaned VHDs

The recommended approach for Azure deployments is to store l resources with the same life cycle in a single resource group. This includes resources like the VMs IP address, NIC Cards, NSG, disks, and the VM itself.

When the VM needs to be deleted, you can simply delete the resource group. This ensures that all the objects associated with the VM are deleted on one go, including the attached disks.

Find and Delete Unused Azure Disks

Now that you know how to prevent creating orphan disks, you should make finding the unused disks in your subscriptions a priority. Azure Advisor can be leveraged to identify the unused VMs in your subscription. The advanced evaluation model in Azure Advisor evaluates parameters such as CPU, memory, and network utilization of VMs and identifies underutilized VMs that can be shutdown or deleted. Deleting the disks along with those VMs will help to keep your cloud charges in control. 

Once you identify where your unused disks are, you can roll up your sleeves and start to delete them. There are two ways of deleting unused disks: using the Azure Portal GUI or PowerShell.

Deleting Unused Disks with the Azure Portal GUI

You can delete both managed and unmanaged disk from Azure portal by following the below steps.

Deleting Managed Disks through the Azure Portal

  1. From Azure portal search for disks.

  2. When the search is complete you’ll see a list of all your disks. Look for any disks that do not have an owner listed.

  3. Select one of the disks without an owner. Check the “Disk state” in the overview tab to make sure that the disk is unattached. If it is, go ahead and click “Delete.”

Deleting Unmanaged Disks through the Azure Portal

  1. From Azure portal, browse to the storage container that contains unmanaged disks.

  2. Click on the disk to view the properties. Ensure that the lease status is “unlocked” and that the lease state is “Available.” This indicates that the disk is not attached to any VMs. Click on Delete to remove the disks.

Deleting Unused Disks with PowerShell

Although there are GUI options to delete unused disks, it becomes cumbersome as you scale your deployments . For unmanaged disks,, you have to drill down to each storage account and blob container or individual VHDs which might become hectic.

Also, sometimes you would notice that the locked lease of the disks are not released automatically (and hence giving false negatives on whether the disk is unused) and there is a whole article on troubleshooting that. This is why a lot of people prefer using scripting tools like PowerShell to find and delete these unused VHDs.

A PowerShell script is the best choice to delete these unused disks. These scripts can be run by logging in to Azure Cloud Shell.

Deleting Managed Disks with PowerShell

Run the following PowerShell commands in sequence:

  1. Set the value of the following variable to 0 if you just want to view the id of the disk. Set it to 1, if you want the disk to be deleted
$deleteOrphanedDisks=1
  1. Get details of managed disk using the following command
$managedDisks = Get-AzDisk
  1. Check for ManagedBy property associated with the disk. The value is null if the managed disk is not attached to the VM. Depending on the value set for $deleteOrphanedDisks, the disks will either be deleted or the Ids of the disks will be listed
foreach ($disk in $managedDisks) {
    if($disk.ManagedBy -eq $null){
        if($deleteOrphanedDisks -eq 1){
            Write-Host "Deleting orphaned Managed Disk with Id: $($disk.Id)"
            $disk | Remove-AzDisk -Force
            Write-Host "Deleted orphaned Managed Disk with Id: $($disk.Id) "
        }else{
            $disk.Id
        }
    }
 }

Deleting Unmanaged Disks with PowerShell

  1. Set the value of the following variable to $false if you just want to view the id of the disk. Set it to $true, if you want the disk to be deleted. 

It is recommended to run the script with initially the value set to $false to see the disks and then run the commands again with value set to $true for deleting the disks:

$deleteOrphanedDisks=$false
  1. Get details of storage account by entering:
$storageAccounts = Get-AzStorageAccount
  1. Run the following code to get details of all page blobs with extension .vhd that can be used as unmanaged disks.The code will check the LeaseStatus property of the page blob. The status for orphaned disks that can be deleted will be unlocked:
$storageKey
  $containers = Get-AzStorageContainer -Context $context
  foreach($container in $containers){
      $blobs = Get-AzStorageBlob -Container $container.Name -Context $context
      $blobs | Where-Object {$_.BlobType -eq 'PageBlob' -and $_.Name.EndsWith('.vhd')} | ForEach-Object { 
          if($_.ICloudBlob.Properties.LeaseStatus -eq 'Unlocked'){
                  if($deleteOrphanedDisks){
                      Write-Host "Deleting unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"
                      $_ | Remove-AzStorageBlob -Force
                      Write-Host "Deleted unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"
                  }
                  else{
                      $_.ICloudBlob.Uri.AbsoluteUri
                  }
          }
      }
  }

More Ways to Reduce Azure Storage Costs

Deleting unused managed and unmanaged Azure disks is an easy way to keep your Azure storage costs in check. Having said that, it is recommended to err on the side of caution when deleting a disk since they might contain important data. Ensure that you have a backup of the data before you start deleting anything.

While deleting orphaned disks is one way of keeping cloud storage cost in check, you can bring in additional cost efficiency for your Azure deployment and disk management by leveraging Cloud Volumes ONTAP. 

Cloud Volumes ONTAP brings the capabilities of NetApp storage management solution to the cloud. Cloud Volumes ONTAP has built-in storage efficiency features such as thin provisioning, deduplication, and compression that reduce the overall amount of storage being used, thereby bringing down your monthly Azure costs. These features are also complimented by other enterprise-class features that ensure Azure high availability, easy Azure migration, performance, agility, and data protection. You can protect the data in your disks through point-in-time NetApp Snapshot™ copies to recover from data corruption and the FlexClone® feature creates writable clones of disk volumes with minimal impact on storage cost.  

The cloud is supposed to be cost-effective, so don’t let your costs get out of control. By deleting unused disks using the steps mentioned in this blog and employing the storage efficiency features of Cloud Volumes ONTAP you’ll have a clear solution to keeping Azure storage costs under control.