Search code examples
azureazure-cliazure-resource-group

Is there a way to remove a resource group depending on tags with Runbook/automation script?


I have a runbook PowerShell that deletes ALL resource groups and everything on them but with the name of the RG. Is there a way to do the same but only with tags that are associate to that RG? Instead of asking me for a NAMEFILTER PARAMETER when running the runbook, it could ask for the TAG? I have been looking everywhere but I can't find a way to delete a resource group by tag without including/using the name.


Solution

  • It's very dangerous to run scripts like this but it's like this

    Get-AzResourceGroup -Tag @{ TagName="TagValue" } | Remove-AzResourceGroup 
    

    Test this locally first and later add -force parameter to remove without prompt

    In case of older modules you might need to run AzureRM alias

    Get-AzureRmResourceGroup -Tag @{ TagName="TagValue" } | Remove-AzureRmResourceGroup