Created
October 24, 2023 18:08
-
-
Save sabbour/d03304d79fca66a78a1b3c45a82e4a59 to your computer and use it in GitHub Desktop.
Deletes empty resource groups using Azure CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get a list of all resource groups in the subscription | |
resource_groups=$(az group list --query '[].name' -o tsv) | |
# Loop through the list of resource groups | |
for rg in $resource_groups | |
do | |
# Check if the resource group has any resources | |
resources=$(az resource list --resource-group $rg --query '[].id' -o tsv) | |
if [ -z "$resources" ] | |
then | |
# Print the name of the empty resource group | |
echo "Deleting empty resource group: $rg" | |
az group delete -n $rg --no-wait --yes | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment