Azure Delete unattached disks

Avatar de Vinicius

Today’s post will cover Delete unattached disks and is part of the Azure Savings series of that cover ways to save money on Azure.

Once I heard, and used ever since this analogy, that using cloud resources is similar to use electricity: the more you use, the more expensive it will be. Although, you can also save money if you apply to the cloud what you currently do for electricity

Turn off things that are not in use!

Other posts from Azure Savings series
Azure Savings Scheduled Shutdown and Startup of Virtual Machines

Storage Type

Azure offers different Storage Types for different purposes, and to storage Virtual Machines disks you will find Managed Disks and Unmanaged Disks (page blobs)

Despite the fact Unmanaged Disks are still supported by Azure, the recommendation is to use Managed Disks as they provide High Fault tolerance, Industry-leading availability and Greated scale.

How do I get unattached disks?

By default, when you delete a virtual machine its disks are not deleted.  Accordingly to Microsoft, this helps to prevent data loss due to the unintentional deletion of VMs.

How unattached disks are billed?

Unnatached disks are billed even though they are not being in use by a Virtual Machine, because you are using storage space. In order to avoid surprises, you should be aware of these costs.

azure cost management cost analysis storage price 1 day blog vinicius deschamps

The image above demonstrate the cost for one day of a Standard HDD Managed Disk, US$0.88/day (~US$26.4/month).

IMPORTANT: The costs may vary, depending the Storage Type, Storage Tier and Storage Account Type (for unmanaged disks)

When I should delete unattached disks?

Since the unnatached disks are a result of deleted Virtual Machines, and Microsoft says this helps to prevent data loss, I’d recommend to identify the disks and determine you no longer need then because the deletion is a permanent action and cannot be undone.

How to identify unattached disks?

You can check through Azure Portal or using Powershell, and if you are unsure about Managed Disks and Unmanaged Disks it is also possible to check both

Azure Portal

[trp_language language=”en_US”]

[trp_language language=”pt_BR”]



[trp_language language=”en_US”]

[trp_language language=”pt_BR”]


Powershell Managed Disks

# Source https://docs.microsoft.com/en-us/azure/virtual-machines/windows/find-unattached-disks#managed-disks-find-and-delete-unattached-disks
# Set deleteUnattachedDisks=1 if you want to delete unattached Managed Disks
# Set deleteUnattachedDisks=0 if you want to see the Id of the unattached Managed Disks
$managedDisks = Get-AzDisk
foreach ($md in $managedDisks) {
    # ManagedBy property stores the Id of the VM to which Managed Disk is attached to
    # If ManagedBy property is $null then it means that the Managed Disk is not attached to a VM
    if($md.ManagedBy -eq $null){
        if($deleteUnattachedDisks -eq 1){
            Write-Host "Deleting unattached Managed Disk with Id: $($md.Id)"
            $md | Remove-AzDisk -Force
            Write-Host "Deleted unattached Managed Disk with Id: $($md.Id) "

Powershell Unmanaged Disks

# Source https://docs.microsoft.com/en-us/azure/virtual-machines/windows/find-unattached-disks#unmanaged-disks-find-and-delete-unattached-disks
# Set deleteUnattachedVHDs=$true if you want to delete unattached VHDs
# Set deleteUnattachedVHDs=$false if you want to see the Uri of the unattached VHDs
$storageAccounts = Get-AzStorageAccount
foreach($storageAccount in $storageAccounts){
    $storageKey = (Get-AzStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName)[0].Value
    $context = New-AzStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageKey
    $containers = Get-AzStorageContainer -Context $context
    foreach($container in $containers){
        $blobs = Get-AzStorageBlob -Container $container.Name -Context $context
        #Fetch all the Page blobs with extension .vhd as only Page blobs can be attached as disk to Azure VMs
        $blobs | Where-Object {$_.BlobType -eq 'PageBlob' -and $_.Name.EndsWith('.vhd')} | ForEach-Object { 
            #If a Page blob is not attached as disk then LeaseStatus will be unlocked
            if($_.ICloudBlob.Properties.LeaseStatus -eq 'Unlocked'){
                        Write-Host "Deleting unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"
                        $_ | Remove-AzStorageBlob -Force
                        Write-Host "Deleted unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"

And that’s it

I hope you liked, and I’ll see you on my next post

Photo by Sharon McCutcheon on Unsplash

Avatar de Vinicius

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *