Skip to content

Instantly share code, notes, and snippets.

@cobola
Created February 9, 2022 04:14
Show Gist options
  • Save cobola/7b239116445437f1f82fffa1b95a9c37 to your computer and use it in GitHub Desktop.
Save cobola/7b239116445437f1f82fffa1b95a9c37 to your computer and use it in GitHub Desktop.
batch remove cloudflare dns records
let axios = require('axios')
let zone = 'test.com'
let head = {
'X-Auth-Email': 'test@gmail.com',
'X-Auth-Key': 'test123',
'Content-Type': 'application/json',
}
async function getZoneID() {
try {
let res = await axios.get('https://api.cloudflare.com/client/v4/zones?name=' + zone + '&status=active', { headers: head })
console.log(res.data)
zoneid = res.data.result[0].id
return zoneid
} catch (error) {
console.error(error)
}
}
async function getDnsRecords(zoneid) {
try {
let res = await axios.get('https://api.cloudflare.com/client/v4/zones/' + zoneid + '/dns_records', { headers: head })
console.log(res.data)
return res.data.result
} catch (error) {
console.error(error)
}
}
async function removeDnsRecord(zoneid, dnsid) {
try {
let res = await axios.delete('https://api.cloudflare.com/client/v4/zones/' + zoneid + '/dns_records/' + dnsid, { headers: head })
console.log(res.data)
return res.data.result
} catch (error) {
console.error(error)
}
}
async function batchDeleteCloudFlareDnsRecords() {
let zoneid = await getZoneID()
let list = await getDnsRecords(zoneid)
for (let index = 0; index < list.length; index++) {
const element = list[index]
console.log(element)
await removeDnsRecord(zoneid, element.id)
}
}
batchDeleteCloudFlareDnsRecords()
@VadimBesedin
Copy link

Hi Cobo La,

thank you for the script. A lot of people looking for that solution.
But how do you run it?

If i run it in VS Code using Node.js it throws an error:
"AxiosError {message: 'Request failed with status code 400', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: ClientRequest, …}"

And here:
'X-Auth-Email': 'test@gmail.com',
'X-Auth-Key': 'test123',
do i need to enter my CloudFlare account login data or API token?

@cobola
Copy link
Author

cobola commented Jul 9, 2022

yes, you need paste the email and key there.

@mocap-lt
Copy link

mocap-lt commented Jul 9, 2022

Thank you.

@StitiFatah
Copy link

StitiFatah commented Nov 19, 2022

Thanks man it works as intended although if one has a lot of records to delete he'd have to run it multiple times to make that many DELETE requests because of their throttling

@moemoks
Copy link

moemoks commented Oct 15, 2023

Totoally new to scripting, How would I run it, Im using mac

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