Skip to content

Instantly share code, notes, and snippets.

@zgunz42
Created December 12, 2020 05:53
Show Gist options
  • Save zgunz42/12227d3e355cc8245394ece47a44d8d5 to your computer and use it in GitHub Desktop.
Save zgunz42/12227d3e355cc8245394ece47a44d8d5 to your computer and use it in GitHub Desktop.
bulk delete dns record cloudflare
const axios = require('axios').default;
const _ = require('lodash');
const email = '<email>';
const key = '<global_key>';
const zone = '<zone>'
const url = `https://api.cloudflare.com/client/v4/zones/${zone}/dns_records`
function deleteRec(dns) {
axios.delete(url + `/${dns.id}`, {
headers: {
'Content-Type': 'application/json',
'X-Auth-Email': email,
'X-Auth-Key': key,
}
}).then(async res => {
const body = res.data;
if (body.result && body.result.id) {
console.log(`delete dns record ${body.result.id}`)
}
})
}
function getPage(index=1, content='72.18.131.4', per_page=100) {
return axios.get(url + `?page=${index}&content=${content}&per_page=${per_page}`, {
headers: {
'Content-Type': 'application/json',
'X-Auth-Email': email,
'X-Auth-Key': key,
}
}).then(async res => {
const body = res.data
if (body.success) {
if (body.result_info.page === body.result_info.total_pages) {
return body.result;
}
const page = await getPage( body.result_info.page + 1 );
return _.flatten([body.result, ...page]);
}
return [];
}).catch((e) => console.log(e))
}
async function main() {
const result = await getPage();
const records = result.map((dns) => deleteRec(dns))
return Promise.all(records);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment