A record deleted after I make API call to update TXT record

For Workers & Pages, what is the name of the domain?

What is the error number?

N/A

What is the error message?

N/A

What is the issue or error you’re encountering

After I called the API to update a TXT record, the first A record in my list of DNS records gets deleted. I can reproduce it every time I run the API! Why is this happening? I checked the audit log and there is no record of it being deleted!

What steps have you taken to resolve the issue?

Run the following command and the first DNS A record is deleted!

curl -X PUT “https://api.cloudflare.com/client/v4/zones/YOUR_ZONE_ID/dns_records/YOUR_DNS_RECORD_ID
-H “Authorization: Bearer YOUR_API_TOKEN”
-H “Content-Type: application/json”
–data ‘{
“type”: “TXT”,
“name”: “example.com”,
“content”: “new_txt_value”,
“ttl”: 120,
“proxied”: false
}’

Are you using the right DNS_RECORD_ID? You aren’t using the Update API call but the Overwrite API call, so it’s expected to delete the old record.

I’ve tried calling this endpoint a few times, and it works as expected.

2 Likes

I modified the script to use PATCH. Same issue. Creating new TXT or updating existing TXT causes the first A record in my list to be deleted and audit logs does not show this. The script now reads as follows:

#!/bin/bash

ZONE_ID=“xyz”
RECORD_NAME=“example.com
API_TOKEN=“123”

RECORD_ID=$(curl -s -X GET “https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?name=$RECORD_NAME
-H “Authorization:Bearer $API_TOKEN”
-H “Content-Type: application/json” | jq -r ‘.result[0].id’)

echo $RECORD_ID

TXT_DATA=“v=spf1 mx ip4:1.2.3.4 ~all”
NAME=“mail”

curl -X PATCH “https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID
-H “Authorization: Bearer $API_TOKEN”
-H “Content-Type: application/json”
–data ‘{“type”:“TXT”,“name”:"’“$NAME”‘“,“content”:”’“$TXT_DATA”‘",“ttl”:120,“proxied”:false}’

RECORD_ID=$(curl -s -X GET “https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?name=$RECORD_NAME”

result[0].id

That looks like you are picking the first DNS record, which is in line with the behaviour you describe. I assume the A record you delete has the same name as the TXT record you want to update?

Ideally, you would look the RECORD_ID up once and hardcode it in your script. It shouldn’t ever change.

2 Likes

ok it certainly does look like it. Thanks, I’ll modify it to choose the correct record.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.