Created
December 12, 2020 05:53
-
-
Save zgunz42/12227d3e355cc8245394ece47a44d8d5 to your computer and use it in GitHub Desktop.
bulk delete dns record cloudflare
This file contains hidden or 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
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