Skip to content

Instantly share code, notes, and snippets.

@ozziexsh
Created January 12, 2023 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ozziexsh/cc186bf4e548cf8db52cdc428743c9ea to your computer and use it in GitHub Desktop.
Save ozziexsh/cc186bf4e548cf8db52cdc428743c9ea to your computer and use it in GitHub Desktop.
Deletes all records in a cloudflare dns zone. Useful for when you import a domain into cloudflare and it adds a bunch of useless dns records that you can't bulk delete
const fetch = require("node-fetch");
function baseRequest(url, options) {
return fetch(`https://api.cloudflare.com/client/v4/${url}`, {
headers: {
Authorization: "Bearer YOUR_TOKEN_HERE",
},
...options,
});
}
function getRecords() {
return baseRequest("zones/YOUR_ZONE_ID/dns_records")
.then((res) => res.json())
.then((res) => res.result);
}
function deleteRecord(recordId) {
return baseRequest(
`zones/YOUR_ZONE_ID/dns_records/${recordId}`,
{ method: "delete" }
);
}
async function main() {
let result;
while ((result = await getRecords())?.length > 0) {
console.log(`fetched ${result.length} records`);
for (const record of result) {
console.log(`deleting ${record.id}`);
await deleteRecord(record.id);
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment