Skip to content

Instantly share code, notes, and snippets.

@peterjaffray
Last active May 3, 2020 18:32
Show Gist options
  • Save peterjaffray/9b1e680905dd2446a8440df077eb3e28 to your computer and use it in GitHub Desktop.
Save peterjaffray/9b1e680905dd2446a8440df077eb3e28 to your computer and use it in GitHub Desktop.
r53 wipe zone
#!/bin/bash
# ./r53-wipe domain.com
# chmod +x r53-wipe.sh
# sudo apt install jq
# Description: This is a basic script to remove hosted zones from r53. It's a real pain in the ass when you have to do it manually.
for domain in "$@"; do
echo "removing $domain and it's records ... "
hosted_zone_id=$(aws route53 list-hosted-zones --output text --query 'HostedZones[?Name==`'$domain'.`].Id')
$VERBOSE && echo hosted_zone_id=${hosted_zone_id:?Unable to find: $domain}
aws route53 list-resource-record-sets --hosted-zone-id $hosted_zone_id |
jq -c '.ResourceRecordSets[]' |
while read -r resourcerecordset; do
read -r name type <<<$(jq -r '.Name,.Type' <<<"$resourcerecordset")
if [[ $type != "NS" || $type != "SOA" ]]; then
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')
echo "removed $type $name $change_id"
fi
done
change_id=$(aws route53 delete-hosted-zone \
--id $hosted_zone_id \
--output text \
--query 'ChangeInfo.Id')
echo "removed hosted zone for $domain_to_delete $change_id"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment