Skip to content

Instantly share code, notes, and snippets.

@slayer
Last active March 21, 2024 10:17
Show Gist options
  • Star 67 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save slayer/442fa2fffed57f8409e0b23bd0673a92 to your computer and use it in GitHub Desktop.
Save slayer/442fa2fffed57f8409e0b23bd0673a92 to your computer and use it in GitHub Desktop.
Delete all DNS records for specified zone
#!/bin/bash
TOKEN="xxxxxxxxxxxxxxxxxxx"
ZONE_ID=2222222222222222222222222
# EMAIL=me@gmail.com
# KEY=11111111111111111111111111
# Replace with
# -H "X-Auth-Email: ${EMAIL}" \
# -H "X-Auth-Key: ${KEY}" \
# for old API keys
curl -s -X GET https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?per_page=500 \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" | jq .result[].id | tr -d '"' | (
while read id; do
curl -s -X DELETE https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${id} \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json"
done
)
@NicklasAndersson
Copy link

Simple and it works!

@moerazem
Copy link

Nice! Works like a charm.

For Mac users, you'll probably need to install jq package.
brew install jq

@kylefmohr
Copy link

This script has worked a number of times for me, thanks!

Just wanted to share an error I got, and what the solution was, in case anybody makes the same mistake I did:

I started getting the following error, even though I've used the script successfully in the past:

jq: error (at <stdin>:1): Cannot iterate over null (null)

The solution: roll (renew) your Cloudflare API token! Mine was invalid, and once I rolled it, the script started working again.

@AidasK
Copy link

AidasK commented Dec 28, 2021

I have a simpler approach, just paste some JS in your browser console:

https://gist.github.com/AidasK/9550e1eb97b3b121c5122aef0d778608

@a-neagoe
Copy link

thank you, your script is working fine!

@armendzz
Copy link

thank you <3

@esbenr
Copy link

esbenr commented Apr 15, 2022

I have a simpler approach, just paste some JS in your browser console:

https://gist.github.com/AidasK/9550e1eb97b3b121c5122aef0d778608

Thanks, this seem like the easiest soæution. All though I can only get the script to delete the current record.
Can you elaborate on where I paste and fire the script?

Standing on the DNS-page it doesent do much.

@witalijx
Copy link

I love you

@mkostrikin
Copy link

To get Zone ID using domain name
ZONE_ID=$(curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type:application/json" -X GET "https://api.cloudflare.com/client/v4/zones?name=$domain"| jq -r '.result[] | {id} | .id')

@Billcountry
Copy link

Thank you, does exactly what I need it to do

@fazio91
Copy link

fazio91 commented Jul 6, 2022

awesome!!! <3

@EML-github
Copy link

Worked a treat; thanks. But note that almost nobody will have 'jq' installed, or have even heard of it. On Deb/Ubuntu, 'apt install jq'.

@mkostrikin
Copy link

Initial code has jq so I've used as well. But agree that extra tool could be missed at user's shell.

@pr4ns
Copy link

pr4ns commented Jan 29, 2023

do you have script to delete specific domain ?

@kylefmohr
Copy link

do you have script to delete specific domain ?

@pr4ns sure, I wrote this for you based on the Cloudflare REST API docs here

#!/bin/bash


# permission needed: #zone:edit
TOKEN="xxxxxxxxxxxxxxxxxxx"
ZONE_ID=2222222222222222222222222


# EMAIL=me@gmail.com
# KEY=11111111111111111111111111
# Replace with 
#     -H "X-Auth-Email: ${EMAIL}" \
#     -H "X-Auth-Key: ${KEY}" \
# for old API keys

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}" \
     -H "Authorization: Bearer ${TOKEN}" \
     -H "Content-Type: application/json"

Tested and working 👍

@yuriybeck
Copy link

thanks a lot. it works for me

@artembokhan
Copy link

Thanks

@bjzhush
Copy link

bjzhush commented Apr 28, 2023

works perfect and thanks a lot !

@aianus
Copy link

aianus commented Jul 23, 2023

Thank you! Ridiculous that you can't add a new zone without copying all the old DNS records from the previous domain owner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment