Skip to content

Instantly share code, notes, and snippets.

@ramizpolic
Created June 10, 2020 16:54
Show Gist options
  • Save ramizpolic/5d4337e4b7f5a74c85518a882b5fd44f to your computer and use it in GitHub Desktop.
Save ramizpolic/5d4337e4b7f5a74c85518a882b5fd44f to your computer and use it in GitHub Desktop.
Delete DNS zone and its records at Google Cloud Platform
#!/bin/bash
set -eo pipefail
DNS_ZONE_NAME="example-dns-zone"
echo -e "Destroying dns zone ($DNS_ZONE_NAME)...\n"
# Purge all DNS records
DNS_RECORDS=$(gcloud dns record-sets list --zone=$DNS_ZONE_NAME --format=json | jq '. |
map(select(.type != ("NS") and .type != ("SOA")))')
gcloud dns record-sets transaction start --zone=$DNS_ZONE_NAME
for row in $(echo "${DNS_RECORDS}" | jq -r '.[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
gcloud dns record-sets transaction remove "$(_jq '.rrdatas[0]')" \
--name=$(_jq '.name') --ttl=$(_jq '.ttl') --type=$(_jq '.type') \
--zone=$DNS_ZONE_NAME
done
gcloud dns record-sets transaction execute --zone=$DNS_ZONE_NAME
# Delete DNS zone
gcloud dns managed-zones delete $DNS_ZONE_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment