Cloudflare delete all DNS entries from new Domain Website

Deleting records, especially when there have been hundreds added automatically to your newly added Domain in Cloudflare, can be extremely time consuming. With a little trick via API, this can be done much faster, in this tutorial I will show you exactly how you can delete all records without spending hours.

What you will need

1st Step - Get the Dns Zone Id

If you have already your API-Key, then we can start right away. Otherwise look here how to get it: HERE

Open Postman and grab your DNS zones, in our example our domain name is MyName.com, you can also leave it out, if you want to list all your domains

 

				
					https://api.cloudflare.com/client/v4/zones?name=MyName.com
				
			

Insert your API-Token in the Auth Tab and select Bearer Token

What you will get as result will look like this:

				
					{
    "result": [
        {
            "id": "290387nt923847f59274f59h82734fh5987",
            "name": "MyName.com",
            "status": "active",
            "paused": false,
            "type": "full",
            "development_mode": 0,
            "name_servers": [
                "brianna.ns.cloudflare.com",
                "scott.ns.cloudflare.com"
            ],
            "original_name_servers": [
                "ns42.domaincontrol.com",
                "ns41.domaincontrol.com"
            ],
				
			

We are only interested in the id, note it down.

Lets assume ours is 20921u908j2349fj829348jf98234h

2nd Step - List all DNS Records

In the second step we want to list all records of the domain that we just selected. On a sidenote, Cloudflare only send 100 records at a time, we will come back later to this topic.

Add a new API Call with Method “GET”

Write the ID you got previously after the the zone/[ID]/dns_records

				
					https://api.cloudflare.com/client/v4/zones/20921u908j2349fj829348jf98234h/dns_records
				
			

Follow the same step as above to set your Bearer for Authentication.

Now go to Tests Tab and write the following code. Note that Console.log can be left out, it’s just for ourselves to follow up with the progress.

 

				
					var jsonData = pm.response.json();
console.log(jsonData.result.length);
var _arr = [];
if(jsonData.result.length) {
    for(var i = 0; i<jsonData.result.length-1; i++){
        _arr.push(jsonData.result[i].id);
    }
    console.log("Deleting records: "+JSON.stringify(_arr));
    pm.collectionVariables.set("recordsToDelete", _arr);
    postman.setNextRequest("DNS Delete Record");
}
				
			

You need to save this API Call with a Name, we take “DNS List Records” and save it into our Collection “Cloudflare”.

The naming is used in postman.setNextRequest and will come in handy to automate what we are doing.

3rd Step - Delete all Records

In the third step we want to delete the records that we have listed above. Add a new API-Call, with Method “DELETE”

Write the ID you got previously after the the zone/[ID]/dns_records/

Note we will use a variable after /dns_records/{{recordToDelete}} leave it exactly like that.

				
					https://api.cloudflare.com/client/v4/zones/20921u908j2349fj829348jf98234h/dns_records/{{recordToDelete}}
				
			

Go to the Tests tab of this API call

Note: We cannot iterate through the complete array and call the API, we use a little trick, we delete only the first record and set the next iteration of our Test-Cycle as the same step “DNS Delete Record”, it will run until the array is empty.

				
					const _arr = pm.collectionVariables.get("recordsToDelete");

if(_arr.length>0){
    const mid = _arr[0];
    _arr.shift();
    pm.collectionVariables.set("recordsToDelete", _arr);
    pm.collectionVariables.set("recordToDelete", mid);
    console.log("Deleting record: "+mid);
    postman.setNextRequest("DNS Delete Record");
}
				
			

Save your API-Call and name it DNS Delete Record

 

4th Step - Run the Collection Test

Now that we have setup the basics, please order your Collection so that DNS List Records, is first and DNS Delete Record comes after.

Then click on your collection to open it up.

Now you can run the collection:

You will be asked again for the order and to click on the Run button

Click on “Run Cloudflare”.

 

IMPORTANT NOTE: As mentioned above the first API does only give us 100 records per call, that’s why if you need to clear more than a 100 records, you will need to iterate this again and Run it again. Just run it until it doesnt fetch any more records.

Hope you enjoyed our Tutorial, if you liked it, rate us or add us on LinkedIn.

Shopping Cart

Free Instant SEO Audit

Powered by

CONTACT US

Scroll to Top