How to remove all CloudFlare DNS (bulk remove)
CloudFlare doesn’t allow you to delete all the DNS settings from their admin console, but you can do so through their API : https://api.cloudflare.com/
Writing script for this is boring. I prefer a Postman collection, it’s the simplest and easiest to launch 😊
- Create a CloudFlare API key here.
- Get Postman collection here, ready to use.
- Configure collection variables.
- Read and configure script into the second request (DNS List records — Tab Tests). I have created a filter by IP, but you can delete it if it is not necessary for you.
- Run into Postman Collection Runner.
Details
For each request, we must add 2 authentication headers : X-Auth-Email and X-Auth-Key.
We need 2 requests : list dns entries, remove an entry
GET https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records
How to find `zone_identifier` ? List zones here.
{
"result": [
{
"id": "jdnjeid91829hbdejza092019398232",
"type": "A",
"name": "abc1.sinao.fr",
"content": "90.124.128.119",
"proxiable": true,
"proxied": true,
"ttl": 1,
"locked": false,
"zone_id": "1291dejnzd0921njndjende1921291d",
"zone_name": "sinao.fr",
"modified_on": "2020-03-20T19:02:13.943853Z",
"created_on": "2020-03-20T19:02:13.943853Z",
"meta": {
"auto_added": false,
"managed_by_apps": false,
"managed_by_argo_tunnel": false
}
},
...
We need `id` for next step.
In the “Tests” tab, we must write a piece of code to pass their parameters to the next request :
var jsonData = pm.response.json();if(jsonData.result.length) {
// I want to delete the DNS for this IP address only for my case
var ipToDelete = '90.124.128.119';
// We are looking for the first occurrence of this IP in the content
for(var i = 0; jsonData.result[i].content !== ipToDelete && i < jsonData.result.length-1; i++);
// If an occurrence has been found, we prepare the next request
if(i < jsonData.result.length-1) {
// Save id into collectionVariables in Postman
pm.collectionVariables.set("recordToDelete", jsonData.result[i].id);
postman.setNextRequest("DNS Delete record");
} else
// Stop bulk
postman.setNextRequest(null);
}
DELETE https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records/:identifier
The result is just a success. So we push postman to execute the previous query to find the next occurrence to delete. And so on until there is no more result.