Skip to content

Instantly share code, notes, and snippets.

@justmiles
Created November 2, 2018 16:15
Show Gist options
  • Save justmiles/4ae3d3148001cbead6a4e3dee392a849 to your computer and use it in GitHub Desktop.
Save justmiles/4ae3d3148001cbead6a4e3dee392a849 to your computer and use it in GitHub Desktop.
deletes all records in a Route53 hosted zone saving a backup to your home directory
#!/bin/bash
#
# purge-route53-zone.sh
# deletes all records in a Route53 hosted zone
# saving a backup to your home directory
#
# Usage:
# purge-route53-zone.sh HOSTED_ZONE_ID
#
# Requirements:
# aws-cli
# jq
#
HOSTED_ZONE=$1
# setup workspace
cd `mktemp -d`
aws route53 list-resource-record-sets --hosted-zone-id $HOSTED_ZONE \
| jq -c '.ResourceRecordSets[] | select((.Type != "NS") and (.Type != "SOA")) | {"Action":"DELETE","ResourceRecordSet":.}' > records
cp records ~/$HOSTED_ZONE.zone-backup
split -l 500 records
rm -rf records
ls | while read line; do
echo "purging $(wc -l $line | grep -o "[0-9]*") records"
record_sets=`mktemp --suffix="-$line"`
jq -s '{"Comment": "purging hosted zone","Changes":.}' $line > $record_sets
aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE --change-batch=file://$record_sets
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment