Skip to content

Instantly share code, notes, and snippets.

@rajivharlalka
Created May 5, 2023 15:49
Show Gist options
  • Save rajivharlalka/ddaa5d7a1aea53c6a6be49f11be7e1a3 to your computer and use it in GitHub Desktop.
Save rajivharlalka/ddaa5d7a1aea53c6a6be49f11be7e1a3 to your computer and use it in GitHub Desktop.
Python script that bulk delete all DNS records. Since CF does not provide an api to delete all records, this script can do the same easily. CF_ZONE_ID and CF_AUTH_ID are two environment variables required for this script.
import requests
import json
import os
zone_id = os.environ['CF_ZONE_ID']
token='Bearer '+ os.environ['CF_AUTH_TOKEN']
authentication={'Authorization' : token}
list_url="https://api.cloudflare.com/client/v4/zones/{0}/dns_records".format(zone_id)
delete_url="https://api.cloudflare.com/client/v4/zones/{0}/dns_records/{1}"
## get no of pages from request list_url using request module
response=requests.get(list_url,headers=authentication)
no_of_page=response.json()['result_info']['total_pages']
## loop through all pages and delete all records
for i in range(1,no_of_page+1):
response=requests.get(list_url,headers=authentication)
result=response.json()['result']
for j in result:
delete_uri=delete_url.format(zone_id,j['id'])
print(delete_uri)
response=requests.delete(delete_uri,headers=authentication)
print(response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment