Deleting Route 53 Zones That Contain Records:

Route 53 zones cannot be deleted if they contain records and there’s not a quick easy way to do purge a domain other than something like this. Pass in your zone id, and this will handle the rest.

#!/bin/bash
define(){ IFS='\n' read -r -d '' $1 || true; }

JSON_FILE=`mktemp`
ZONE_ID=$1

for i in `aws --no-verify-ssl route53 list-resource-record-sets --hosted-zone-id "$ZONE_ID" | jq -c '.[][]' | grep -Ev 'SOA|NS'`; do

# This is beacuse of artifact with ssl intercepts at my org. Wont hurt to leave it
echo "$i" | grep "Name" > /dev/null
if [ $? != 0 ]; then
    continue
fi

DNS_NAME=`echo "$i" | jq -r .Name` 
RECORD_TYPE=`echo "$i" | jq -r .Type` 
TTL=`echo "$i" | jq -r .TTL` 
RESOURCE_RECORDS=`echo "$i" | jq -r .ResourceRecords` 

define DELETE_JSON <<EOC
cat <<EOF > $JSON_FILE
{
    "Comment": "Delete single record set",
    "Changes": [
        {
            "Action": "DELETE",
            "ResourceRecordSet": {
                "Name": "$DNS_NAME",
                "Type": "$RECORD_TYPE",
                "TTL": $TTL,
                "ResourceRecords": $RESOURCE_RECORDS
            }
        }
    ]
}
EOF
EOC

eval "$DELETE_JSON"

aws --no-verify-ssl route53 change-resource-record-sets --hosted-zone-id "$ZONE_ID" --change-batch file://$JSON_FILE

aws --no-verify-ssl route53 delete-hosted-zone --id $ZONE_ID

done