Skip to content

Instantly share code, notes, and snippets.

@Voronenko
Created October 11, 2016 15:22
Show Gist options
  • Save Voronenko/76713700d7443cd8a2d1680fbb32ec75 to your computer and use it in GitHub Desktop.
Save Voronenko/76713700d7443cd8a2d1680fbb32ec75 to your computer and use it in GitHub Desktop.
Route53 delete zone
def delete_zone(domain):
""" Deleting a hosted zone in AWS is a real pain if you have any number of
resource records defined because you must first delete all RR's in order to
remove the zone. This is a one-stop script to do all of that in one command.
"""
import boto
from boto.route53.record import ResourceRecordSets
PROTECTED_DOMAINS = ['foo.com', 'bar.net']
if domain in PROTECTED_DOMAINS:
print "NO DELETING " + domain + "!!!!"
exit(1)
conn = boto.connect_route53()
zone = conn.get_zone(domain.lower().strip('.'))
changes = ResourceRecordSets(conn, zone.id)
records = zone.get_records()
for record in records:
if record.type != "NS" and record.type != "SOA":
change = changes.add_change_record("DELETE", record)
if len(changes.changes) > 0:
result = changes.commit()
zone.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment