Created
January 12, 2023 00:40
-
-
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
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 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