V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
estk
V2EX  ›  Cloudflare

Cloudflare 批量删除 DNS 记录的方式

  •  
  •   estk · 2022-07-12 21:48:19 +08:00 · 2960 次点击
    这是一个创建于 647 天前的主题,其中的信息可能已经有所发展或是发生改变。

    域名 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 条回复    2022-12-09 12:10:55 +08:00
    wdssmq
        1
    wdssmq  
       2022-07-13 14:43:02 +08:00
    我之前是用官方的 PHP SDK 封装了个用。。(╯▽╰)
    estk
        2
    estk  
    OP
       2022-07-13 16:17:58 +08:00 via Android
    @wdssmq
    有的大神直接 shell 脚本
    tofdoo
        3
    tofdoo  
       2022-07-29 17:26:12 +08:00
    CF 的 API 支持二次开发 dns 接口吗?重新封装后让用户可以自己改的需求(也在啃文档...)
    czyt
        4
    czyt  
       2022-12-09 12:10:55 +08:00
    @tofdoo golang 的是可以的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   931 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 20:29 · PVG 04:29 · LAX 13:29 · JFK 16:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.