Skip to content

Instantly share code, notes, and snippets.

@Laiteux
Last active May 12, 2024 08:53
Show Gist options
  • Save Laiteux/bc505c5350421c31f8bd448bdacb6ff8 to your computer and use it in GitHub Desktop.
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.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace CloudflareDnsCleaner
{
// NOTE: Old, idk if it still works or anything
public static class Program
{
private const string AuthKey = "YOUR_AUTH_KEY"; // Global API Key: https://dash.cloudflare.com/profile/api-tokens
private const string AuthEmail = "YOUR_AUTH_EMAIL"; // Account Email: https://dash.cloudflare.com/profile
private const string ZoneId = "YOUR_ZONE_ID"; // Zone ID: Can be found on your website dashboard
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)
{
const int perPage = 100;
using var responseMessage = await httpClient.GetAsync($"dns_records?per_page={perPage}");
var responseJson = JsonSerializer.Deserialize<JsonElement>(await responseMessage.Content.ReadAsStringAsync());
var records = responseJson.GetProperty("result").EnumerateArray().ToList();
foreach (var record in records)
{
await httpClient.DeleteAsync($"dns_records/{record.GetProperty("id").GetString()}");
}
if (records.Count - perPage < 0)
{
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment