How to find and delete the orphan disks in Azure

Azure

In my today’s post I will show you how you can easily delete orphan resources in Azure. Let’s see how it works, in any enterprise you may have multiple subscription. Once you login to Azure the first step is to set the subscription in which you would like to delete the orphan resources. You can use the below command to do the same.

a1

Now I will remove one VM from one of the resource group as shown below.

a2

Please note that the VM is deleted but the VM disks are still there, so in this case there were two disks in the VM one of them is a data disk and the other is the OS disk. To find the disk I will list all the disks present in the subscription as shown below

vm

Once I have identified the data disk which is part of that VM, I can delete it with below command.

Same thing will be applied for the OS disk which is also I can delete as shown below.

vm3

Now it’s may be possible that you may have more orphan resources present in the portal. First check the VM’s present now

vm4

Now in next step we will check all the Orphan resources present in Azure, as we can see there are many Orphan disks which are still present and incurring bill.

vm5

Now I can write a script  which will find the list of all the orphan disks and delete them as you can see below

$Disk=Get-AzDisk
$Orphan=$Disk | Select-Object -Property Name,ResourceGroupName,Type,DiskSizeGB,DiskState
$a=$Orphan | Where-Object -Property DiskState -eq “Unattached”
Foreach ($disks in $a)
{
$ResourceGroup=$disks.ResourceGroupName
$DiskName=$disks.Name
Remove-AzDisk -ResourceGroupName $ResourceGroup -DiskName $DiskName -Force
}
az

Once you run the script and it has completed all the deletion of the disks, you can again run the below command to list the disks present in your subscription and you can see all of them are reserved. So no more Orphan Disks in your subscription.

az2

I hope you like this post, due to heavy work pressure, I can’t give much time to my blog posts as before however I will try to post few more articles on Azure cost optimization technique like this, which can helps you to control your Azure Spend.

Thanks for reading wish you have a great day ahead.