![Azure](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/azure-growth.png)
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](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan1.png)
Now I will remove one VM from one of the resource group as shown below.
![a2](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan2.png)
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](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan3.png)
Once I have identified the data disk which is part of that VM, I can delete it with below command.
![](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan4.png)
Same thing will be applied for the OS disk which is also I can delete as shown below.
![vm3](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan5.png)
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](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan6.png)
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](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan7.png)
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](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan8.png)
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](https://cdn-0.reconshell.com/wp-content/uploads/2021/03/Howtofindan9.png)
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.
Leave a Reply