Cloudflare is a popular content delivery network (CDN) and DNS provider that offers a range of security, performance, and reliability services for websites. As a website owner, you might need to delete all DNS records on Cloudflare for various reasons, such as moving to a different DNS provider or cleaning up outdated records. In this article, we’ll show you how to delete all Cloudflare DNS records using PHP.
Why Use PHP?
PHP is a widely used server-side programming language that can interact with various APIs and web services. Using PHP, you can send HTTP requests to the Cloudflare API and process the responses to perform various operations, such as creating, updating, or deleting DNS records. PHP is also easy to learn and use, even for beginners, and runs on most web servers.
How to Delete All Cloudflare DNS Records Using PHP
To delete all Cloudflare DNS records using PHP, you need to follow these steps:
Step 1: Obtain Cloudflare API Key and Email
To use the Cloudflare API, you need to obtain an API key and email address associated with your Cloudflare account. You can get your API key from the Cloudflare dashboard by navigating to My Profile > API Tokens > Global API Key. Make sure to copy your API key and keep it secure, as it provides full access to your Cloudflare account.
Step 2: Identify Your Domain and Zone ID
Next, you need to identify the domain name and zone ID associated with the DNS records you want to delete. The domain name is the primary domain name of your website, such as example.com. The zone ID is a unique identifier assigned by Cloudflare to each domain zone. You can obtain the zone ID by sending a GET request to the Cloudflare API with your domain name and API key. Here’s an example PHP code snippet to get the zone ID:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // Replace with your Cloudflare API key and email $api_key = "YOUR_API_KEY"; $email = "YOUR_EMAIL"; // Replace with the domain you want to delete DNS records for $domain = "example.com"; // Get zone ID for domain $zone_id = ""; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.cloudflare.com/client/v4/zones?name={$domain}&status=active", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "X-Auth-Email: {$email}", "X-Auth-Key: {$api_key}", "Content-Type: application/json" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { $response = json_decode($response, true); if ($response['success']) { $zone_id = $response['result'][0]['id']; } else { echo "Failed to get zone ID"; exit; } } |
This code sends a GET request to the Cloudflare API with your domain name and API key, and extracts the zone ID from the response if it’s successful. You can then use this zone ID to delete all DNS records associated with the domain zone.
Step 3: Delete All DNS Records for the Zone
Finally, you need to send a DELETE request to the Cloudflare API to delete all DNS records associated with the zone ID. Here’s an example PHP code snippet to delete all DNS records for the zone:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // Delete all DNS records for zone $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https;//api.cloudflare.com/client/v4/zones/{$zone_id}/dns_records", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "DELETE", CURLOPT_HTTPHEADER => array( "X-Auth-Email: {$email}", "X-Auth-Key: {$api_key}", "Content-Type: application/json" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { $response = json_decode($response, true); if ($response['success']) { echo "All DNS records deleted successfully for {$domain}"; } else { echo "Failed to delete DNS records for {$domain}"; } } |
This code sends a DELETE request to the Cloudflare API with the zone ID and API key, and checks if the response is successful. If the response is successful, all DNS records associated with the zone ID are deleted, and a success message is displayed. Otherwise, an error message is displayed.
Adding Try-Catch Blocks for Error Handling
In the above code snippets, we used conditional statements to check for errors and display appropriate messages. However, you can also use try-catch blocks to handle errors more elegantly and gracefully. Here’s an example PHP code snippet with try-catch blocks for error handling:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?php try { // Replace with your Cloudflare API key and email $api_key = "YOUR_API_KEY"; $email = "YOUR_EMAIL"; // Replace with the domain you want to delete DNS records for $domain = "example.com"; // Get zone ID for domain $zone_id = ""; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.cloudflare.com/client/v4/zones?name={$domain}&status=active", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "X-Auth-Email: {$email}", "X-Auth-Key: {$api_key}", "Content-Type: application/json" ), )); $response = curl_exec($curl); if (!$response) { throw new Exception('cURL Error: ' . curl_error($curl)); } $response = json_decode($response, true); if (!$response['success']) { throw new Exception('Failed to get zone ID for ' . $domain); } $zone_id = $response['result'][0]['id']; // Delete all DNS records for zone $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.cloudflare.com/client/v4/zones/{$zone_id}/dns_records", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "DELETE", CURLOPT_HTTPHEADER => array( "X-Auth-Email: {$email}", "X-Auth-Key: {$api_key}", "Content-Type: application/json" ), )); $response = curl_exec($curl); if (!$response) { throw new Exception('cURL Error: ' . curl_error($curl)); } $response = json_decode($response, true); if (!$response['success']) { throw new Exception('Failed to delete DNS records for ' . $domain); } echo "All DNS records deleted successfully for {$domain}"; } catch (Exception $e) { echo "Error: " . $e->getMessage(); } |
This code wraps the entire process in a try block and catches any exceptions thrown by the code in the catch block. The exceptions can be thrown by any of the API calls, such as the cURL request to get the zone ID, or the cURL request to delete the DNS records. If an exception is caught, the catch block is executed and an error message is displayed. This makes it easier to identify and debug errors in the code.