Last active
May 12, 2024 08:53
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| 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) | |
| { | |
| 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(); | |
| foreach (var record in records) | |
| { | |
| await httpClient.DeleteAsync(string.Join('/', "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