Skip to content

Instantly share code, notes, and snippets.

@sabbour
Created October 24, 2023 18:08
Show Gist options
  • Save sabbour/d03304d79fca66a78a1b3c45a82e4a59 to your computer and use it in GitHub Desktop.
Save sabbour/d03304d79fca66a78a1b3c45a82e4a59 to your computer and use it in GitHub Desktop.
Deletes empty resource groups using Azure CLI
#!/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