estk

Cloudflare 批量删除 DNS 记录的方式

  •  
  •   estk · Jul 12, 2022 · 4070 views
    This topic created in 1435 days ago, the information mentioned may be changed or developed.

    域名 NS 之前在阿里云,解析了泛域名*.domain.com

    导入 cf 时,它自动帮我同步了 200+条记录

    尝试在阿里云删除*.domain.com,等了十几分钟再导入依然 200+记录

    尝试了几次发现没有阻止 cf 自动导入的方式,于是打算手动删除

    但是 200+条太多,官方也没有批量删除的方式,只好借助 api 了

    import axios from 'axios'
    
    const apiEmail = 'cloudflare login email'
    const apiKey   = 'Global API Key @ https://dash.cloudflare.com/profile/api-tokens'
    const zoneId   = 'zone id of the domain in cloudflare'
    
    const api = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`
    const headers = {'X-Auth-Email': apiEmail, 'X-Auth-Key': apiKey}
    axios.request({url: `${api}?per_page=5000`, headers}).then(({data})=>{ 
      // https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records at List DNS Records
      let errorOccurs = false
      for (const dnsRecord of data.result||[]) {
        axios.request({url: `${api}/${dnsRecord.id}`, method: 'DELETE', headers}).then(({data})=>{
          console.log('delete result', data)
        }).catch(error=>{
          console.error(error, error.response?.data)
          errorOccurs = true
        })
        if (errorOccurs) break
      }
    }).catch(error=>{
      console.error(error, error.response?.data)
    })
    
    4 replies    2022-12-09 12:10:55 +08:00
    wdssmq
        1
    wdssmq  
       Jul 13, 2022
    我之前是用官方的 PHP SDK 封装了个用。。(╯▽╰)
    estk
        2
    estk  
    OP
       Jul 13, 2022 via Android
    @wdssmq
    有的大神直接 shell 脚本
    tofdoo
        3
    tofdoo  
       Jul 29, 2022
    CF 的 API 支持二次开发 dns 接口吗?重新封装后让用户可以自己改的需求(也在啃文档...)
    czyt
        4
    czyt  
       Dec 9, 2022
    @tofdoo golang 的是可以的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5360 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 60ms · UTC 07:32 · PVG 15:32 · LAX 00:32 · JFK 03:32
    ♥ Do have faith in what you're doing.