-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (60 loc) · 1.87 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
const request = require("request");
const zoneId = ""; // Get your zone ID from https://dash.cloudflare.com/ navigate to your site that you transfer over and then look for a zone ID.
const domainName = ""; // Not required, was used for debugging.
const apiKey = ""; // Required, get it at https://dash.cloudflare.com/profile/api-tokens you need a API KEY and NOT a API TOKEN.
const email = ""; // Required, your email adress that you use to login into cloudflare.
// Get all DNS records for a zone
const getRecords = () => {
const options = {
method: "GET",
url: `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records?page=1&per_page=100`,
headers: {
"X-Auth-Email": email,
"X-Auth-Key": apiKey,
"Content-Type": "application/json"
}
};
return new Promise((resolve, reject) => {
request(options, function (error, response, body) {
if (error) {
reject(error);
return;
}
resolve({ response, body });
});
});
};
// Delete a DNS record
const deleteRecord = recordId => {
const options = {
method: "DELETE",
url: `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${recordId}`,
headers: {
"X-Auth-Email": email,
"X-Auth-Key": apiKey,
"Content-Type": "application/json"
}
};
return new Promise((resolve, reject) => {
request(options, function (error, response, body) {
if (error) {
reject(error);
return;
}
resolve({ response, body });
});
});
};
const deleteAllRecords = async () => {
try {
const { body } = await getRecords();
const records = JSON.parse(body).result;
for (const record of records) {
await deleteRecord(record.id);
}
console.log("bulk records deleted successfully");
} catch (error) {
console.error(`Error deleting records: ${error.message}`);
}
};
deleteAllRecords();