Bulk delete DNS records on import

Hi!

Please make it possible to bulk delete DNS records when importing a site.
I have taken over a domain with > 200 DNS records, now need to click 400 times to get rid of them all :wink:

I used this script to do it in C# .NET 6.0. You need to remove the spaces in the Uri part (I had to add the spaces so I could submit it here)

ā€˜ā€™'csharp
using System.Net.Http.Headers;
using Newtonsoft.Json;

const string token = ā€œCloudflare_tokenā€;
const string zone = ā€œCloudflare_website_zoneā€;

var client = new HttpClient();
client.BaseAddress = new Uri(ā€œhttps :// api.Cloudflare .com /client/v4/ā€);

var msg = new HttpRequestMessage(HttpMethod.Get, $ā€œzones/{zone}/dns_records?per_page=300ā€);
msg.Headers.Add(ā€œAuthorizationā€, $ā€œBearer {token}ā€);
msg.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(ā€œapplication/jsonā€));

var result = await client.SendAsync(msg);
// Newtonsoft JsonCovert (System.Text.Json can not handle dynamic correctly yet)
var data = JsonConvert.DeserializeObject(await result.Content.ReadAsStringAsync());

foreach (var r in data.result)
{
Console.WriteLine(ā€œRecord {0}, name {1}, content {2}, id {3}ā€, r.type, r.name, r.content, r.id);
var delRq = new HttpRequestMessage(HttpMethod.Delete, $ā€œzones/{zone}/dns_records/{r.id}ā€);
delRq.Headers.Add(ā€œAuthorizationā€, $ā€œBearer {token}ā€);
var delResult = await client.SendAsync(delRq);
if (!delResult.IsSuccessStatusCode)
Console.WriteLine($ā€œCould not delete. Status: {delResult.StatusCode} Error: {delResult.ReasonPhrase} {await delResult.Content.ReadAsStringAsync()}ā€);
}
ā€˜ā€™ā€™

I have been struggling with the same issue and built a simple tool to delete DNS records in bulk. Check it out: https://cloudflare-dns-th0th.vercel.app (the source code is here: GitHub - th0th/cloudflare-dns: A simple tool for bulk-deleting DNS records on CloudFlare.)

4 Likes

Thank you!
The code works great!
You saved me a lot of time :slight_smile:
My gratitude will forever haunt you! :wink:

Donā€™t worry anymore, just do this, I deleted 800+ DNS Records in like a minute with this

[Bulk Delete Cloudflare DNS Records]https://ekg.com.np/bulk-delete-cloudflare-dns/

A post was split to a new topic: How do I manage DNS?

This is a godsend! Thank you SO MUCH for creating this and making it available for use.