Skip to content

Instantly share code, notes, and snippets.

@KianNH
Created May 13, 2022 15:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KianNH/1517012cac8db377dee7313618bebaee to your computer and use it in GitHub Desktop.
Save KianNH/1517012cac8db377dee7313618bebaee to your computer and use it in GitHub Desktop.
Cloudflare - Remove all DNS records in a given zone
$API_TOKEN = "<api-token>"
$ZONE_ID = "<zone-id>"
$baseUrl = "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
$headers = @{
'Authorization' = "Bearer $API_TOKEN"
'Content-Type' = "application/json"
}
$listUrl = $baseUrl + '?per_page=500'
$records = Invoke-RestMethod -Uri $listUrl -Method 'GET' -Headers $headers
$records = $records | Select-Object -ExpandProperty result
foreach ($record in $records) {
Write-Host "Deleting $($record.name) that points to $($record.content)"
$deleteUrl = $baseUrl + '/' + $record.id
Invoke-RestMethod -Uri $deleteUrl -Method 'DELETE' -Headers $headers
}
@KianNH
Copy link
Author

KianNH commented May 13, 2022

Obvious note - this will indiscriminately delete all the records it can find in a zone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment