Azure automation: delete empty resource groups automatically

I like to keep my Microsoft Azure subscription clean, but sometimes there are mismatches between the new and the old portal, or even when you remove resources you can leave resource groups completely emptied, making your list of resource groups to grow when in fact there is nothing there.

Empty resource groups
Empty resource groups

Therefore, I’ve created a PowerShell workflow that reviews the resource groups that have in my subscription and if they don’t have any associated resource it removes them.

Before you can run this script it is necessary to upload the latest version of the AzureResourceManager module as I already explained in this previous post. The module is located in C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager and you just have to generate a zip with the AzureResourceManager folder and upload it as a module within Azure Automation account through Assets > Modules > Add Module.

Azure Automation - Add Module
Azure Automation – Add Module – AzureResourceManager.zip

Once uploaded and processed you will see it with the rest of modules available in the account.

Azure Automation Modules - AzureResourceManager
Azure Automation Modules – AzureResourceManager

You’ll also need to add a Credential asset as also shown in the Automate Azure Resource Manager with Azure Automation  post. The new portal is quite similar: Assets Credentials and add your Azure Active Directory user who is also the administrator of your account.

Create a new PowerShell runbook. In my example I’ve called it Delete-Empty-Resource-Groups and I’ve added the following:

workflow Delete-Empty-Resource-Groups
{
	#The name of the Automation Credential Asset this runbook will use to authenticate to Azure.
    $CredentialAssetName = "DefaultAzureCredential";
	
	#Get the credential with the above name from the Automation Asset store
    $Cred = Get-AutomationPSCredential -Name $CredentialAssetName;
    if(!$Cred) {
       	Throw "Could not find an Automation Credential Asset named '${CredentialAssetName}'. Make sure you have created one in this Automation Account."
    }
   	#Connect to your Azure Account   	
	Add-AzureRmAccount -Credential $Cred;
	
	#Get Azure Resource Groups
	$rgs = Get-AzureRmResourceGroup;
	
	if(!$rgs){
		Write-Output "No resource groups in your subscription";
	}
	else{
		
		Write-Output "You have $($(Get-AzureRmResourceGroup).Count) resource groups in your subscription";
		
		foreach($resourceGroup in $rgs){
			$name=  $resourceGroup.ResourceGroupName;
			$count = (Get-AzureRmResource | where { $_.ResourceGroupName -match $name }).Count;
			if($count -eq 0){
				Write-Output "The resource group $name has $count resources. Deleting it...";
				Remove-AzureRmResourceGroup -Name $name -Force;
			}
			else{
				Write-Output "The resource group $name has $count resources";
			}
		}
		
		Write-Output "Now you have $((Get-AzureRmResourceGroup).Count) resource group(s) in your subscription";
		
	}  
}

Once you have completed these steps you can launch the script through the Test pane and check that everything works correctly.

Azure Automation - Test pane
Azure Automation – Test pane

You will see the result of the script and the possible errors before you publish it and schedule it.

Azure Automation - Test pane - Delete Empty Resource Groups
Azure Automation – Test pane – Delete Empty Resource Groups

If you prefer not to create the runbook from scratch, I‘ve uploaded the script to the Gallery to make it available for everyone.

Azure Automation - Browse Gallery
Azure Automation – Browse Gallery

Cheers!