Delete Azure Resource Group with Orphan Resources in it

This issue will appear if deployment will fail on Azure then there might be some orphan resources available .Sometimes those resources will not able to delete from Azure Portal.

I have faced same issue while deploying blockchain ordering service on Azure and I got below error.

I tried couple of times to delete azure Resource group from portal but got error

I was able to fix issue by applying below command.

$allResourceGroups = Get-AzureRmResourceGroup | ForEach-Object { $_.ResourceGroupName }

$resourceGroupsWithResources = Get-AzureRMResource | Group-Object ResourceGroupName | ForEach-Object { $_.Name }

$emptyResourceGroups = $allResourceGroups | Where-Object { $_ -NotIn $resourceGroupsWithResources } 

$emptyResourceGroups | ForEach-Object { Remove-AzureRmResourceGroup -Name $_ -Force }

Here they are packaged as functions that can be called

Function Get-AzureRmResourceGroupsWithNoResources {
    process {
        $allResourceGroups = Get-AzureRmResourceGroup | ForEach-Object { $_.ResourceGroupName }

        $resourceGroupsWithResources = Get-AzureRMResource | Group-Object ResourceGroupName | ForEach-Object { $_.Name }

        $emptyResourceGroups = $allResourceGroups | Where-Object { $_ -NotIn $resourceGroupsWithResources } 

        return $emptyResourceGroups
    }
}

Function Remove-AzureRmResourceGroupsWithNoResources {
    process {   
        Get-AzureRmResourceGroupsWithNoResources | ForEach-Object { Remove-AzureRmResourceGroup -Name $_ -Force }
    }
}

Reference – https://stackoverflow.com/questions/40452926/delete-azure-resource-groups-with-no-resources-in-it?rq=1

Leave a Reply

Your email address will not be published. Required fields are marked *