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.
Once you login to Azure, run the powershell script saved in previous step.