Automating Bulk Deletion of Cloudflare DNS Records using Node.js and Axios

Bulk Delete Cloudflare DNS Records


Introduction:

Cloudflare is a popular service that provides various features, including DNS management. As your online presence grows, managing DNS records can become a time consuming task, especially when dealing with numerous records. This article will guide you through the process of automating the bulk deletion of Cloudflare DNS records using a Node.js script powered by the Axios library.

Prerequisites:

Before getting started, ensure you have the following prerequisites installed on your system:

1. Node.js: Download and install Node.js from [nodejs.org](https://nodejs.org/).

2. Axios: Axios is a promise-based HTTP client for the browser and Node.js. Install it using the following command in your terminal or command prompt:

   npm install axios

Script Overview:

The provided Node.js script utilizes the Axios library to interact with Cloudflare’s API for managing DNS records. It consists of three main functions:

1. getDnsRecords: Fetches a list of DNS records associated with your Cloudflare account.

2. deleteDNSRecord: Deletes a specific DNS record identified by its record ID.

3. deleteDNSRecords: Combines the above functions to delete all DNS records associated with your Cloudflare account.

Script Usage:

1. Obtain API Key, Email, and Zone ID:

   – Log in to your Cloudflare account.

   – Navigate to the API section and generate an API key.

   – Note your email associated with the Cloudflare account.

   – Obtain your Zone ID from the Cloudflare dashboard.

2. Insert Credentials:

   – Open the script in a text editor.

   – Replace ‘API_KEY’, ‘YOUR_EMAIL’, and ‘YOUR_ZONE_ID’ with your actual Cloudflare API key, email, and Zone ID, respectively.

3. Run the Script:

   – Open a terminal or command prompt.

   – Navigate to the directory containing the script.

   – Run the script using the following command:

     node scriptFileName.js

   Replace `scriptFileName.js` with the actual name of your script file.

4. Monitor Deletion Process:

   – The script will display information about each deleted record, including the record ID and the Cloudflare API response.

Conclusion:

Automating the bulk deletion of Cloudflare DNS records using this Node.js script can save time and effort, especially when managing a large number of records. Regularly cleaning up unused or unnecessary records contributes to a more organized and efficient DNS management system. Feel free to customize the script further to suit your specific requirements or integrate it into your existing workflow.

Script:

const axios = require(‘axios’);

const apiKey = ‘API_KEY‘;
const email = ‘YOUR_EMAIL‘;
const zoneId = ‘YOUR_ZONE_ID‘;

const apiUrl = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`;

const getDnsRecords = async () => {
const headers = {
‘X-Auth-Email’: email,
‘X-Auth-Key’: apiKey,
‘Content-Type’: ‘application/json’,
};

let allRecords = [];
let page = 1;

while (true) {
try {
const response = await axios.get(apiUrl, {
params: { page },
headers,
});

if (response.status === 200) {
const records = response.data.result.map(record => record.id);
allRecords = allRecords.concat(records);

// Check if there are more pages
if (response.data.result_info && response.data.result_info.page < response.data.result_info.total_pages) {
page++;
} else {
break;
}
} else {
console.error(`Failed to get DNS records. Status: ${response.status}, Error: ${JSON.stringify(response.data.errors)}`);
break;
}
} catch (error) {
console.error(`Error: ${error.message}`);
break;
}
}

return allRecords;
};

const deleteDNSRecord = async (recordId) => {
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${recordId}`;
const headers = {
‘Content-Type’: ‘application/json’,
‘X-Auth-Key’: apiKey,
‘X-Auth-Email’: email,
};

try {
const response = await axios.delete(url, { headers });
console.log(`Record ID ${recordId} deleted successfully. Response:`, response.data);
} catch (error) {
console.error(`Error deleting record ID ${recordId}:`, error.response ? error.response.data : error.message);
}
};

const deleteDNSRecords = async () => {
const dnsRecordIds = await getDnsRecords();

if (dnsRecordIds.length > 0) {
console.log(‘Deleting DNS Records:’);
for (const recordId of dnsRecordIds) {
await deleteDNSRecord(recordId);
}
} else {
console.log(‘No DNS records found.’);
}
};

deleteDNSRecords();

Bulk Delete Cloudflare DNS
Bulk Delete Cloudflare DNS
10-c-programs-for-neb-students
10 C Program for Students with Algorithm and Flowchart
np domain
How to Get a .com.np Domain for Free in Nepal: 4 Easy Steps
Xera Control Panel by mahtab2003 SMTP Error Fix
How to Fix SMTP Email Issues in Xera and MOFH-R : 2 Easy Steps
SQLite Database in Java
SQLite Database in Java (Intellij Setup)
Crazy Games - Saurav
Crazy Games
Top Online Gaming Sites - Saurav Phuyal
Top Online Gaming Sites
Recent Videos
Recent Posts
10 C Program for Students with Algorithm and Flowchart
How to Get a .com.np Domain for Free in Nepal: 4 Easy Steps
How to Fix SMTP Email Issues in Xera and MOFH-R : 2 Easy Steps
SQLite Database in Java (Intellij Setup)
Bulk Delete Cloudflare DNS Records using Node.js and Axios
Leave a Reply

Your email address will not be published. Required fields are marked *