Created
January 19, 2022 10:04
-
-
Save 0xKayvan/576e386b02b536a75ad6843ce32375eb to your computer and use it in GitHub Desktop.
This file contains 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
import axios, { AxiosRequestConfig } from 'axios' | |
const domainName = 'example.com' | |
const auth = 'Bearer POT_YOUR_TOKEN_HERE'; | |
(async () => { | |
try { | |
let requestConfig: AxiosRequestConfig = { | |
url: `https://api.cloudflare.com/client/v4/zones?name=${domainName}`, | |
method: 'GET', | |
headers: { | |
'Authorization': auth, | |
'Content-Type': 'application/json' | |
} | |
} | |
const getZoneResponse = await axios(requestConfig) | |
if (!getZoneResponse || !getZoneResponse.data || getZoneResponse.data.result.length < 1 || !getZoneResponse.data.result[0].id) { | |
throw new Error('not found') | |
} | |
const zone = getZoneResponse.data.result[0].id | |
requestConfig = { | |
url: `https://api.cloudflare.com/client/v4/zones/${zone}/dns_records`, | |
method: 'GET', | |
headers: { | |
'Authorization': auth, | |
'Content-Type': 'application/json' | |
} | |
} | |
const dnsRecords: string[] = [] | |
const response = await axios(requestConfig) | |
const responseData = response.data.result | |
console.log(responseData) | |
if (Array.isArray(responseData)) { | |
responseData.forEach(record => { | |
dnsRecords.push(record.id) | |
}) | |
} | |
console.log(`Number of records to delete: ${dnsRecords.length}`) | |
for (let i = 0; i < dnsRecords.length; i++) { | |
const id = dnsRecords[i] | |
const requestConfig: AxiosRequestConfig = { | |
url: `https://api.cloudflare.com/client/v4/zones/${zone}/dns_records/${id}`, | |
method: 'DELETE', | |
headers: { | |
'Authorization': auth, | |
'Content-Type': 'application/json' | |
} | |
} | |
await axios(requestConfig) | |
console.log(`${i} deleted`) | |
} | |
console.log('Done') | |
} catch (error) { | |
console.log(error) | |
} | |
})() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment