Remove empty Resource Groups in Azure Microsoft

Remove empty Resource Groups in Azure

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

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-AzureResourceGroup

foreach($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
    }
}

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish