Created
February 9, 2022 04:14
-
-
Save cobola/7b239116445437f1f82fffa1b95a9c37 to your computer and use it in GitHub Desktop.
batch remove cloudflare dns records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
yes, you need paste the email and key there.
Thank you.
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
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
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?