Deleting multiple unattached azure disks in bulk

Razzi29 311 Reputation points
2022-04-05T13:44:40.763+00:00

My question is broken into two sections, related to Azure disks that are unattached/ no owner. 1s question: Besides using the Azure portal to check which disks have no owner associated with them, is there another way I can check or run a script for secondary sanity checks to verify which disks are no longer in use? 2. Question: Once I populate the Disk name in an Excel spreadsheet, how I can delete it in bulk so I don't have to go through the portal one by one?

Azure Storage Explorer
Azure Storage Explorer
An Azure tool that is used to manage cloud storage resources on Windows, macOS, and Linux.
229 questions
Azure Disk Encryption
Azure Disk Encryption
An Azure service for virtual machines (VMs) that helps address organizational security and compliance requirements by encrypting the VM boot and data disks with keys and policies that are controlled in Azure Key Vault.
159 questions
Azure Disk Storage
Azure Disk Storage
A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.
571 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Pietro Visentin 1 Reputation point
    2022-04-05T15:00:57.487+00:00

    Hello Razzi,
    I think you might find this article useful.
    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/find-unattached-disks#managed-disks-find-and-delete-unattached-disks

    First, after connecting with powershell to your Azure instance, run the script by setting the deleteUnattachedDisks variable to 0. This action lets you find and view all the unattached managed disks.
    After you review all the unattached disks, run the script again and set the deleteUnattachedDisks variable to 1. This action lets you delete all the unattached managed 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  
    $deleteUnattachedDisks=0  
    $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) "  
            }else{  
                $md.Id  
            }  
        }  
     }  
    

    Hope this will help.

    Best


  2. Razzi29 311 Reputation points
    2022-04-06T13:08:05.4+00:00

    @Pietro Visentin This link is definitely useful for deleting every disk that is not attached, but I want to be specific in the deletion, that is why I am trying to find a way to have a script read from a csv where I am specifying a few disks I want to delete in bulk.