Skip to content

Instantly share code, notes, and snippets.

@andrewvmail
Forked from NoTimeForHero/index.js
Created March 29, 2020 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewvmail/522032af4c55d57c83756056a3a816ab to your computer and use it in GitHub Desktop.
Save andrewvmail/522032af4c55d57c83756056a3a816ab 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'
}
const a = 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`);
}
}
a();
{
"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