Skip to content

Instantly share code, notes, and snippets.

@Laiteux
Last active May 12, 2024 08:53
Show Gist options
  • Select an option

  • Save Laiteux/bc505c5350421c31f8bd448bdacb6ff8 to your computer and use it in GitHub Desktop.

Select an option

Save Laiteux/bc505c5350421c31f8bd448bdacb6ff8 to your computer and use it in GitHub Desktop.
Ready-to-use C# script to delete all DNS records from a Cloudflare domain
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace CloudflareDNSCleaner
{
public static class Program
{
private const string AuthKey = "YOUR_AUTH_KEY";
private const string AuthEmail = "YOUR_AUTH_EMAIL";
private const string ZoneId = "YOUR_ZONE_ID";
public static async Task Main()
{
var httpClient = new HttpClient()
{
BaseAddress = new Uri($"https://api.cloudflare.com/client/v4/zones/{ZoneId}/")
};
httpClient.DefaultRequestHeaders.Add("X-Auth-Key", AuthKey);
httpClient.DefaultRequestHeaders.Add("X-Auth-Email", AuthEmail);
while (true)
{
using var responseMessage = await httpClient.GetAsync("dns_records");
var responseJson = JsonSerializer.Deserialize<JsonElement>(await responseMessage.Content.ReadAsStringAsync());
if (responseJson.GetProperty("result_info").GetProperty("total_count").GetInt16() == 0)
{
break;
}
foreach (JsonElement record in responseJson.GetProperty("result").EnumerateArray())
{
await httpClient.DeleteAsync("dns_records/" + record.GetProperty("id").GetString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment