Skip to content

Instantly share code, notes, and snippets.

@NoTimeForHero
Created July 10, 2019 17:47
Show Gist options
  • Save NoTimeForHero/d9cd8ee51a8f198fb970f87dcae5018e to your computer and use it in GitHub Desktop.
Save NoTimeForHero/d9cd8ee51a8f198fb970f87dcae5018e to your computer and use it in GitHub Desktop.
NodeJS script to remove 1000 DNS records added by Cloudflare when used * (wildcard) A record
// Now if you adding a domain with wildcard A record, Cloudflare uses strange scan, which added a 1000 trash domains (like 1-100, some english words like "ai", "air", "android").
// There's no way to bulk delete it, you can delete it only using their API.
// So I write a script that can help you with this problem.
// Discussions about same problem:
// https://community.cloudflare.com/t/delete-all-records-using-api/13410/2
// https://community.cloudflare.com/t/bulk-delete-dns-record/89540
const settings = {
email: 'your@email',
key: 'your_api_key',
domain: 'domain.example'
}
(async() => {
const cloudflare = require('cloudflare')({email: settings.email, key: settings.key});
const zone_id = await cloudflare.zones.browse().then(zones => zones.result.find(x => x.name === settings.domain).id);
const info = await cloudflare.dnsRecords.browse(zone_id).then(x => x.result_info);
console.log(`Pages to delete: ${info.total_pages}`);
for (let i=0;i<info.total_pages;i++) {
const dns_records = await cloudflare.dnsRecords.browse(zone_id).then(x => x.result.map(dns_record => dns_record.id));
await Promise.all(dns_records.map(id => cloudflare.dnsRecords.del(zone_id, id)));
console.log(`Page ${i+1} is deleted remained ${info.total_pages-i} pages`);
}
})();
{
"name": "cloudflare_truncate_zone",
"main": "index.js",
"author": "NoTimeForHero",
"license": "MIT",
"dependencies": {
"cloudflare": "^2.4.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment