Skip to content

Instantly share code, notes, and snippets.

@matt-FFFFFF
Last active October 6, 2023 13:59
Show Gist options
  • Save matt-FFFFFF/f9b88c4195c0cc0c51b56a7277415472 to your computer and use it in GitHub Desktop.
Save matt-FFFFFF/f9b88c4195c0cc0c51b56a7277415472 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to clean up after Enterprise-Scale - DO NOT RUN IN PRODUCTION
echo "THIS SCRIPT WILL DESTROY YOUR AZURE ENVIRONMENT"
read -n 4 -p "Are you SURE you want to do this? (type 'yes' to continue): " YES
if [ ! "$YES" == "yes" ]; then
echo "Confirmation denied - quitting"
exit 0
fi
TENANT_ID=$(az account list | jq -r '.[] | select(.isDefault) | .tenantId')
recurse_delete_mg() {
echo "Recurse delete $1"
az account management-group show --name $1 \
--expand \
| jq '.children[] | select(.type=="Microsoft.Management/managementGroups") | .name' \
| xargs -P5 -I% bash -c "recurse_delete_mg %"
az account management-group delete --name $1
}
echo 'Moving subscriptions into root management group'
az account list --refresh --all \
| jq -r --arg TENANTID "$TENANT_ID" '.[] | select(.homeTenantId==$TENANTID) | .id' \
| xargs -I% -P5 az account management-group subscription add --name $TENANT_ID --subscription %
echo 'Removing tenant deployments'
az deployment tenant list | \
jq -r '.[].name' | \
xargs -n 1 -P 10 az deployment tenant delete --name
export -f recurse_delete_mg
echo "Removing management groups"
az account management-group show --name $TENANT_ID \
--expand \
| jq -r '.children | .[] | select(.type=="Microsoft.Management/managementGroups") | .name' \
| xargs -P5 -I% bash -c "recurse_delete_mg %"
unset -f recurse_delete_mg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment