Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

Delete unattached Managed disks in Azure using Powershell

Posted on August 17, 2019February 27, 2022

Update : updated script to support Azure Az powershell module.

While deleting Azure Virtual Machines, we sometimes don’t delete managed disks attached to the VM. This could be intentional as we may want to attach it to another VM or could be just because we forgot, or did not set the option to delete managed disks while deleting the VM.

Whatever the reason it may be, end of the day it costs money, especially if it is a large Premium disk.

Following script will help you deleting a list of unattached disks. I am providing a list of disks as I may not want to delete all unattached managed disks. Someone might be mad if I delete his managed disk which he left there for a purpose 🙂 .

Before start using this script, make sure, you have Azure Az Module installed and imported on the system. For more information on how to install and configure Az module refer following article : https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.5.0

Param(
    [Parameter(Mandatory=$true)]
    [String]$diskListPath
)
$date = Get-Date -UFormat "%m-%d-%Y_%H%M%S"
$dFormat = "%m-%d-%Y %H:%M:%S"
$currentDir = $(Get-Location).Path
$tFile = "$($currentDir)\diskDeletion_Transcript_$($date).txt"
$iFile = $diskListPath
if(!(Test-Path $iFile)){
    Write-Host "[$(Get-Date -UFormat $dFormat)] ERROR: Supplied VM Disk list file path is invalid. Provide correct file path and try again."
    Exit
}
Start-Transcript -Path $tFile -Append -NoClobber
#region - run through the list of vm disks and delete those
foreach($disk in Get-Content $iFile){
    $diskDetails = $diskResourceGroup = $managedBy = ""
    Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Checking if $disk is Unattached" -ForegroundColor Green
    $diskDetails = Get-AzResource -Name $disk -ResourceType Microsoft.Compute/disks -ErrorAction SilentlyContinue
    if([string]::IsNullOrEmpty($diskDetails)){
        Write-Host "[$(Get-Date -UFormat $dFormat)] ERROR: Coundn't find the Disk $disk" -ForegroundColor Red
    }
    else{
        $diskResourceGroup = $diskDetails.ResourceGroupName
        $managedBy = if($diskDetails.ManagedBy){$diskDetails.ManagedBy.split("/")[-1]}
        if(!([string]::IsNullOrEmpty($managedBy))){
            Write-Host "[$(Get-Date -UFormat $dFormat)] ERROR: Disk $disk is attached to VM $managedBy. Disk will not be deleted" -ForegroundColor Red
        }
        else{
            #New-AzSnapshotConfig -SourceUri $Disk.Id -CreateOption Copy -Location $Location
            Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Disk $disk is not attached to any VM.Deleting disk $disk" -ForegroundColor Green
            Remove-AzDisk -DiskName $disk -ResourceGroupName $diskResourceGroup -Force -Confirm:$false 
            $diskDetails = Get-AzResource -Name $disk -ResourceType Microsoft.Compute/disks -ErrorAction SilentlyContinue
            if([string]::IsNullOrEmpty($diskDetails)){
                Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Disk $disk has been deleted successfully" -ForegroundColor Green
            }
            else{
                Write-Host "[$(Get-Date -UFormat $dFormat)] ERROR: Failed to delete Disk $disk" -ForegroundColor Red
            }               
        }             
    }
    Write-Host (("=") * 100)
}
Stop-Transcript
#end-region

Download above script and save it with a .ps1 file extension. Open powershell console and, login to your Azure account using – Login-AzAccount. It will prompt you to enter your Azure credentials.

This image has an empty alt attribute; its file name is image-3.png
Login to Azure account

Once you login to Azure, run the powershell script saved in previous step.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
July 2025
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes