Skip to content

Instantly share code, notes, and snippets.

@kmcquade
Last active November 5, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmcquade/512eb33c014ec8279704fa5b6a398b7c to your computer and use it in GitHub Desktop.
Save kmcquade/512eb33c014ec8279704fa5b6a398b7c to your computer and use it in GitHub Desktop.
Nuke every Azure resource group in every Azure subscription
#!/usr/bin/env bash
for sub in `az account list | jq -r '.[].id'`; do \
for rg in `az group list --subscription $sub | jq -r '.[].name'`; do \
az group delete --name ${rg} --subscription $sub --no-wait --yes; \
done; done;
resource "null_resource" "nuke" {
# Because we set this to timestamp, it *always* runs :D
triggers = {
party_like_its_jan_1_1970 = timestamp()
}
provisioner "local-exec" {
command = <<EOF
for sub in `az account list | jq -r '.[].id'`; do \
for rg in `az group list --subscription $sub | jq -r '.[].name'`; do \
az group delete --name $rg --subscription $sub --no-wait --yes; \
done; \
done;
EOF
}
}
# Loop through subscriptions
for sub in `az account list | jq -r '.[].id'`; do
# Loop through resource groups
for rg in `az group list --subscription $sub | jq -r '.[].name'`; do
# Delete every resource group in the subscription
# az group delete --name ${rg} --subscription $sub --no-wait --yes; \
# Use the line below instead to just show the resource group details instead
az group show --name $rg --subscription $sub;
done;
done;
@kmcquade
Copy link
Author

kmcquade commented Nov 4, 2020

Test run (non destructive)

A less destructive alternative: This lets you just show the details behind each resource group.

#!/usr/bin/env bash

subscriptions=$(az account list | jq -r '.[].id')
# Loop through subscriptions
for sub in ${subscriptions}
do
    # Loop through resource groups
    resource_groups=$(az group list --subscription $sub | jq -r '.[].name')
    for rg in ${resource_groups}
    do
        # Delete every resource group in the subscription
        # az group delete --name $rg --subscription $sub --no-wait --yes
        # Use the line below instead to just show the resource group details instead
        az group show --name $rg --subscription $sub
    done
done

Test run one-liner

subscriptions=$(az account list | jq -r '.[].id'); \
    for sub in ${subscriptions}; \
        do \
            resource_groups="$(az group list --subscription ${sub} | jq -r '.[].name')"; \
            for rg in ${resource_groups}; \
                do \
                    az group show --name ${rg} --subscription ${sub}; \
            done; \
    done;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment