手动删除cloudflare的DNS解析记录实在是太费劲了
脚本会使用Cloudflare的API分页功能,每次请求最多100个解析记录,并在完成这批的删除后请求下一批,这样可以确保你只需要运行脚本一次就能删除所有的解析记录。
解决
#!/bin/bash
# Set your Cloudflare API key
API_TOKEN="xxxxxxx"
# Set the zone ID for the domain you want to manage
ZONE_ID="yyyyyyy"
# Initial values
PER_PAGE=100 # Cloudflare API maximum limit per request
while :; do
# Get DNS records for the zone from the first page
RESPONSE=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?per_page=$PER_PAGE&page=1" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-s)
# Check if the result is empty, then break
NUM_RECORDS=$(echo "$RESPONSE" | jq '.result | length')
if [ "$NUM_RECORDS" -eq 0 ]; then
break
fi
# Parse the JSON response to get the record IDs
RECORD_IDS=$(echo "$RESPONSE" | jq -r '.result[].id')
# Loop through the record IDs and delete each one
for RECORD_ID in $RECORD_IDS; do
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-s
echo -e "Deleted record ID: $RECORD_ID\n"
done
done
echo "All records deleted successfully."
评论区