Remove empty Resource Groups in Azure

How to remove all empty Resource Groups in an Azure subscription.

John Savill

October 22, 2015

1 Min Read
Remove empty Resource Groups in Azure
Microsoft

Q. How can I remove all empty Resource Groups in an Azure subscription?

A. You may create resources and a Resource Group is automatically created which are left behind if you delete the resource. Below is some easy PowerShell to delete any Resource Group that is empty. It displays each Resource Group and the number of resources within.

Switch-AzureMode -Name AzureResourceManager$RGs = Get-AzureResourceGroupforeach($RG in $RGs){    $Resources = Get-AzureResource -ResourceGroupName $RG.ResourceGroupName -OutputObjectFormat New    $outputstr = $RG.ResourceGroupName + " - " + $Resources.Length    Write-Output $outputstr    if($Resources.Length -eq 0)    {        Remove-AzureResourceGroup -Name $RG.ResourceGroupName    }}

 

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like