Skip to content

Instantly share code, notes, and snippets.

@nc9
Last active April 14, 2024 15:44
Show Gist options
  • Save nc9/ba4cf63d34a3b6fb6c68d9eb081fd837 to your computer and use it in GitHub Desktop.
Save nc9/ba4cf63d34a3b6fb6c68d9eb081fd837 to your computer and use it in GitHub Desktop.
Script to delete all DNS records for a cloudflare domain
#!/usr/bin/env python
#
# script to delete all dns records for a zone in cloudflare
#
# usage:
# cloudflare_delete_dns.py <domain> [api_token]
#
# api_token is optional, if not provided, it will be read from the CLOUDFLARE_API_TOKEN environment variable
#
# requires cloudflare library, install with
#
# pip install cloudflare
#
import logging
import os
import CloudFlare
logging.basicConfig(level=logging.INFO, format="[%(asctime)s %(levelname)s] %(message)s")
logger = logging.getLogger("cloudflare_delete_dns")
def remove_all_dns_records(domain: str, api_token: str | None = None) -> None:
if not api_token:
api_token = os.getenv("CLOUDFLARE_API_TOKEN")
if not api_token:
logger.error("API token not provided")
return
cf = CloudFlare.CloudFlare(token=api_token)
# Get the zone ID for the domain
try:
zones = cf.zones.get(params={"name": domain})
zone_id = zones[0]["id"]
except CloudFlare.exceptions.CloudFlareAPIError as e:
logger.error(f"Error retrieving zone ID: {e}")
return
# Get all DNS records for the zone
try:
dns_records = cf.zones.dns_records.get(zone_id)
except CloudFlare.exceptions.CloudFlareAPIError as e:
logger.error(f"Error retrieving DNS records: {e}")
return
# Delete each DNS record
for record in dns_records:
record_id = record["id"]
try:
cf.zones.dns_records.delete(zone_id, record_id)
logger.info(f"Deleted DNS record: {record['name']} ({record['type']})")
except CloudFlare.exceptions.CloudFlareAPIError as e:
logger.error(f"Error deleting DNS record: {e}")
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
logger.error(f"Usage: {sys.argv[0]} <domain> [api_token]")
sys.exit(1)
domain = sys.argv[1]
api_token = None
if len(sys.argv) == 3:
api_token = sys.argv[2]
remove_all_dns_records(domain, api_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment