Skip to content

Instantly share code, notes, and snippets.

@memiah-steve
Created April 18, 2018 13:19
Show Gist options
  • Save memiah-steve/0e54a4883bc466960d73373c33638ab5 to your computer and use it in GitHub Desktop.
Save memiah-steve/0e54a4883bc466960d73373c33638ab5 to your computer and use it in GitHub Desktop.
AWS route53 Delete all hosted zones (bash, aws cli, jq)
#!/bin/bash
# AWS route53 Delete all hosted zones.
# Requires aws cli, jq
# chmod u+x ~/aws-route53-delete-hosted-zones.sh
# AWS profile to use.
PROFILE="default"
# Loop through each Hosted Zone.
while read id
do
# Output Hosted Zone ID
echo "$id"
# List all existing Record Sets.
aws route53 list-resource-record-sets --hosted-zone-id "$id" --profile="$PROFILE" | jq -c '.ResourceRecordSets[]' | while read -r resourcerecordset ; do
read -r name type <<<$(echo $(jq -r '.Name,.Type' <<<"$resourcerecordset"))
# Output record type and name
echo "$type : $name"
# Delete any record that is of type NS or SOA.
if [ $type != "NS" -a $type != "SOA" ]; then
aws route53 change-resource-record-sets \
--hosted-zone-id "$id" \
--change-batch '{"Changes":[{"Action":"DELETE","ResourceRecordSet":'"$resourcerecordset"'}]}' \
--output text \
--profile="$PROFILE"
fi
done;
# Delete the Hosted Zone
aws route53 delete-hosted-zone --id "$id" --profile="$PROFILE"
done <<< "`aws route53 list-hosted-zones --profile="$PROFILE" | jq -r '.HostedZones | map(.Id | split("/")[2]) | join("\n")'`"
@RoyBellingan
Copy link

thank you

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