Last active
August 26, 2025 23:57
-
-
Save wrouesnel/1d4d0efac4e6ec086995756d9087f68f to your computer and use it in GitHub Desktop.
Just a copy of https://github.com/alestic/aws-route53-wipe-hosted-zone/blob/master/bin/aws-route53-wipe-hosted-zone - version which handles duplicate zones
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 | |
| # | |
| # DANGEROUS! | |
| # | |
| # aws-route53-wipe-hosted-zone - Delete a Route 53 hosted zone with all contents | |
| # | |
| set -e | |
| VERBOSE=true | |
| delete_record() { | |
| local hosted_zone_id="$1" | |
| local resourcerecordset="$2" | |
| local name | |
| local type | |
| read -r name type <<< "$(jq -r '[.Name, .Type] | @tsv' <<<"$resourcerecordset")" | |
| change_id=$(aws route53 change-resource-record-sets \ | |
| --hosted-zone-id "$hosted_zone_id" \ | |
| --change-batch '{"Changes":[{"Action":"DELETE","ResourceRecordSet": | |
| '"$resourcerecordset"' | |
| }]}' \ | |
| --output text \ | |
| --query 'ChangeInfo.Id') | |
| $VERBOSE && echo "DELETING: $type $name $change_id" | |
| } | |
| for domain_to_delete in "$@"; do | |
| $VERBOSE && echo "DESTROYING: $domain_to_delete in Route 53" | |
| read -r -a hosted_zone_ids < <( | |
| aws route53 list-hosted-zones \ | |
| --output text \ | |
| --query 'HostedZones[?Name==`'$domain_to_delete'.`].Id' | \ | |
| tr -s ' ' | tr '\t' ' ' | |
| ) | |
| for hosted_zone_id in "${hosted_zone_ids[@]}"; do | |
| $VERBOSE && | |
| echo hosted_zone_id="${hosted_zone_id:?Unable to find: $domain_to_delete}" | |
| while read -r resourcerecordset; do | |
| read -r name type <<< "$(jq -r '[.Name, .Type] | @tsv' <<<"$resourcerecordset")" | |
| case "$type" in | |
| SOA) | |
| $VERBOSE && echo "SKIPPING: $type $name" | |
| ;; | |
| NS) | |
| if [ "$name" != "${domain_to_delete}." ]; then | |
| delete_record "$hosted_zone_id" "$resourcerecordset" | |
| else | |
| $VERBOSE && echo "SKIPPING: $type $name" | |
| fi | |
| ;; | |
| *) | |
| delete_record "$hosted_zone_id" "$resourcerecordset" | |
| ;; | |
| esac | |
| done < <( aws route53 list-resource-record-sets --hosted-zone-id "$hosted_zone_id" --output json | jq -c '.ResourceRecordSets[]' ) | |
| change_id=$(aws route53 delete-hosted-zone \ | |
| --id "$hosted_zone_id" \ | |
| --output text \ | |
| --query 'ChangeInfo.Id') | |
| $VERBOSE && echo "DELETING: hosted zone for $domain_to_delete $change_id" | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment