Using Bash script to mass delete Cloudflare DNS records

Cloudflare is a popular platform for managing DNS records for websites and applications. However, if you need to delete a large number of DNS records at once, it can be time-consuming to delete them one by one. In this blog post, we will show you how to delete mass DNS records on Cloudflare using a Bash script.

To get started, you will need the following:

  • You will first need to download jq to use this script.
  • A Cloudflare API token with the necessary permissions to modify DNS records.
  • The ID of the zone that contains the DNS records you want to delete.
  • Your Cloudflare account email address.
  • Your Cloudflare X-Auth-Key. Once you have gathered this information, you can use the following Bash script to delete mass DNS records on Cloudflare:


curl -X GET "${ZONE_ID}/dns_records?page=1&per_page=300" \
	-H "Authorization: Bearer ${TOKEN}" \
        -H "X-Auth-Email: ${EMAIL}" \
        -H "X-Auth-Key: ${X_AUTH_KEY}" \
	-H "Content-Type: application/json"

for dns_record in $(curl -X GET "${ZONE_ID}/dns_records?page=1&per_page=300" \
	-H "Authorization: Bearer ${TOKEN}" \
	-H "X-Auth-Email: ${EMAIL}" \
	-H "X-Auth-Key: ${X_AUTH_KEY}" \
	-H "Content-Type: application/json" | jq -r ".result [] .id")
 curl -X DELETE "${ZONE_ID}/dns_records/$dns_record" \
	-H "Content-Type: application/json" \
	-H "Authorization: ${TOKEN}" \
	-H "X-Auth-Email: ${EMAIL}" \
	-H "X-Auth-Key: ${X_AUTH_KEY}"

Breaking down how the script works

The first few lines of the script define the four variables that we need to make API calls to Cloudflare. You will need to replace the placeholder text with your actual API token, zone ID, email address, and X-Auth-Key.

The next line of the script uses the curl command to make a GET request to the Cloudflare API to retrieve a list of all DNS records for the specified zone. The page and per_page parameters are set to 1 and 300 respectively, which means that the API will return the first 300 DNS records.

The for loop that follows iterates through each DNS record ID in the list of DNS records returned by the API. For each ID, it sends a DELETE request to the Cloudflare API to delete the corresponding DNS record.

The DELETE request uses the curl command with the -X flag set to DELETE, and includes the DNS record ID in the URL.

Finally, the script includes the necessary headers to authenticate the API request with Cloudflare, including the API token, email address, and X-Auth-Key.

Note that this script will only delete the first 300 DNS records for the specified zone. If you have more than 300 DNS records that you need to delete, you will need to modify the page and per_page parameters in the curl command to retrieve additional pages of DNS records.

In conclusion, deleting mass DNS records on Cloudflare can be easily accomplished with the help of a Bash script. By following the steps outlined in this post and customizing the script with your own Cloudflare API token, zone ID, email address, and X-Auth-Key, you can quickly delete large numbers of DNS records without the need for manual intervention. As always, it’s important to exercise caution when making changes to your DNS configuration, so be sure to double-check your settings before running the script.