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.)

2 Likes

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