---
name: azion-change-dns-records-in-bulk
description: >-
  Create Edge DNS records from a JSON file, then update and delete many records of a zone, with shell loops over the Azion API v4.
---

# Change DNS records in bulk

You can create, update, and delete many [Edge DNS](/en/documentation/platform/edge-dns/) records of a zone with shell loops over the [Azion API](/en/documentation/devtools/api/) v4. The records endpoint takes one record per request, so each loop sends one request per record. To add, edit, or delete a single record from Azion Console, the CLI, or the API, refer to [Add, edit, or delete a record](/en/documentation/guides/application-security/dns/add-records/).

---

## Prerequisites

- A personal token. To create one, refer to [Manage personal tokens](/en/documentation/guides/platform/account-and-billing/personal-tokens/).
- Permission to edit Edge DNS. Refer to [Teams permissions](/en/documentation/fundamentals/teams-permissions/).
- `curl`, to call the API, and `jq`, to read the records file and the API responses.
- A zone in Edge DNS. To create one, refer to [Create a zone](/en/documentation/platform/edge-dns/quickstart/#create-a-zone). The examples use a zone for the domain `example.org`.

---

## Find the ID of the zone

Every record path carries the ID of its zone.

To find the ID of the zone:

1. Store your token in a shell variable. Replace the value with your token:

   ```bash
   export AZION_API_TOKEN="[TOKEN VALUE]"
   ```

2. Search the zones for your domain. Replace the domain with your own:

   ```bash
   curl --silent "https://api.azion.com/v4/workspace/dns/zones?fields=id,domain&search=example.org" \
     --header "Authorization: Token $AZION_API_TOKEN"
   ```

   ```json
   {
     "count": 1,
     "total_pages": 1,
     "page": 1,
     "page_size": 10,
     "next": null,
     "previous": null,
     "results": [
       {
         "id": 8068,
         "domain": "example.org"
       }
     ]
   }
   ```

3. Store the `id` of the zone whose `domain` is yours. Replace the value with that `id`, or the shell refuses the line:

   ```bash
   ZONE_ID=<your-zone-id>
   ```

`ZONE_ID` holds the ID of the zone, and every request in this guide reads it.

---

## Create records from a file

A JSON file holds one object per record. Each object needs `name`, `type`, and `rdata`, and this file also sets `ttl`, which defaults to 3,600 seconds. An ANAME record takes a `ttl` of `20` only, so its object sets `"ttl": 20`. For the value format of each record type, refer to [Record types](/en/documentation/platform/edge-dns/record-types/).

> **Caution**
>
> A CNAME or MX value carries no trailing dot. The API refuses one with `400` and code `19002`, `Invalid Domain Name`. For example, it refuses the CNAME value `example.org.` and the MX value `20 mail2.example.org.`.

To create the records:

1. Create a `records.json` file with the records. Replace the names and values with your own:

   ```json
   [
     {
       "name": "www",
       "type": "CNAME",
       "rdata": ["example.org"],
       "ttl": 3600
     },
     {
       "name": "shop",
       "type": "A",
       "rdata": ["192.0.2.10", "192.0.2.11"],
       "ttl": 300
     },
     {
       "name": "api",
       "type": "A",
       "rdata": ["192.0.2.20"],
       "ttl": 300
     },
     {
       "name": "@",
       "type": "MX",
       "rdata": ["10 mail.example.org"],
       "ttl": 3600
     },
     {
       "name": "@",
       "type": "TXT",
       "rdata": ["v=spf1 -all"],
       "ttl": 3600
     }
   ]
   ```

2. Send one `POST` request per record. The loop prints one line per record, with the name and type from the file. The `errors` field of the line holds the error when the API refuses the record, and `null` otherwise. Replace `[TOKEN VALUE]` and `<your-zone-id>` with your own values:

   ```bash
   export AZION_API_TOKEN="[TOKEN VALUE]"
   ZONE_ID=<your-zone-id>
   jq -c '.[]' records.json | while read -r record; do
     curl --silent "https://api.azion.com/v4/workspace/dns/zones/$ZONE_ID/records" \
       --header "Authorization: Token $AZION_API_TOKEN" \
       --header 'Content-Type: application/json' \
       --data "$record" \
     | jq -c --argjson record "$record" '{name: $record.name, type: $record.type, id: .data.id, errors}'
   done
   ```

   ```text
   {"name":"www","type":"CNAME","id":612521,"errors":null}
   {"name":"shop","type":"A","id":612522,"errors":null}
   {"name":"api","type":"A","id":612523,"errors":null}
   {"name":"@","type":"MX","id":612524,"errors":null}
   {"name":"@","type":"TXT","id":612525,"errors":null}
   ```

The zone holds one record per object of the file. A refused record does not stop the loop: its line carries the error, and the next record is sent.

You can run the loop again after you fix a refused record. A name and type hold one record with the [`simple` policy](/en/documentation/platform/edge-dns/how-it-works/#record-policies), the default.

A second one is refused with code `19004`, `Record Already Exists`. For a second CNAME with the same name, the code is `19014`, `CNAME Record Name Conflict`. On a second run of the same file, the loop prints:

```text
{"name":"www","type":"CNAME","id":null,"errors":[{"code":"19014","title":"CNAME Record Name Conflict","detail":"A CNAME with this name already exists for this sub-domain.","status":"400","source":{"pointer":"/data"}}]}
{"name":"shop","type":"A","id":null,"errors":[{"code":"19004","title":"Record Already Exists","detail":"There is already another record matching those data.","status":"400","source":{"pointer":"/data"}}]}
{"name":"api","type":"A","id":null,"errors":[{"code":"19004","title":"Record Already Exists","detail":"There is already another record matching those data.","status":"400","source":{"pointer":"/data"}}]}
{"name":"@","type":"MX","id":null,"errors":[{"code":"19004","title":"Record Already Exists","detail":"There is already another record matching those data.","status":"400","source":{"pointer":"/data"}}]}
{"name":"@","type":"TXT","id":null,"errors":[{"code":"19004","title":"Record Already Exists","detail":"There is already another record matching those data.","status":"400","source":{"pointer":"/data"}}]}
```

Weighted records can share a name and type, so the API accepts a second one. Before you run the loop again, remove the weighted records that already exist from the file.

A name that a resolver queried before its record existed is answered `NXDOMAIN` for up to one hour. To fix it, refer to [A new record returns NXDOMAIN](/en/documentation/platform/edge-dns/troubleshooting/#a-new-record-returns-nxdomain).

---

## Update several records

A loop lists the records, selects the ones to change with `jq`, and sends one `PATCH` request to each. A `PATCH` body carries only the fields to change, so every other field of the record keeps its value.

> **Note**
>
> The list request reads one page of up to 100 records, the most a page holds. In a zone with more records, the loop changes only the records of the first page. To read every page, refer to [Page through a list with filters and ordering](/en/documentation/guides/application-development/automation/page-through-a-list-with-filters-and-ordering/).

Set the time to live (TTL) of every A record of the zone to 600 seconds. Replace `[TOKEN VALUE]` and `<your-zone-id>` with your own values:

```bash
export AZION_API_TOKEN="[TOKEN VALUE]"
ZONE_ID=<your-zone-id>
curl --silent "https://api.azion.com/v4/workspace/dns/zones/$ZONE_ID/records?page_size=100&fields=id,type" \
  --header "Authorization: Token $AZION_API_TOKEN" \
| jq -r '.results[] | select(.type == "A") | .id' \
| while read -r record_id; do
  curl --silent --request PATCH "https://api.azion.com/v4/workspace/dns/zones/$ZONE_ID/records/$record_id" \
    --header "Authorization: Token $AZION_API_TOKEN" \
    --header 'Content-Type: application/json' \
    --data '{"ttl": 600}' \
  | jq -c '{name: .data.name, type: .data.type, ttl: .data.ttl, errors}'
done
```

```text
{"name":"shop","type":"A","ttl":600,"errors":null}
{"name":"api","type":"A","ttl":600,"errors":null}
```

Every A record of the zone has a TTL of 600 seconds, and its other fields keep their values.

A changed record can take a few minutes to reach every nameserver. Resolvers then keep the old value until its TTL runs out.

---

## Delete several records

A loop lists the records, selects every record with a given name, whatever its type, and sends one `DELETE` request to each.

> **Caution**
>
> A deleted record cannot be restored. For a weighted record, first set its weight to `0`. For the steps, refer to [Set a weight of 0 before you delete a weighted record](/en/documentation/platform/edge-dns/best-practices/#set-a-weight-of-0-before-you-delete-a-weighted-record).

> **Note**
>
> The list request reads one page of up to 100 records, the most a page holds. In a zone with more records, the loop deletes only the matching records of the first page. To read every page, refer to [Page through a list with filters and ordering](/en/documentation/guides/application-development/automation/page-through-a-list-with-filters-and-ordering/).

To delete the records named `api` and `shop`:

1. List the records named `api` and `shop` before you delete them. Replace `[TOKEN VALUE]` and `<your-zone-id>` with your own values:

   ```bash
   export AZION_API_TOKEN="[TOKEN VALUE]"
   ZONE_ID=<your-zone-id>
   curl --silent "https://api.azion.com/v4/workspace/dns/zones/$ZONE_ID/records?page_size=100&fields=id,name,type,rdata,ttl" \
     --header "Authorization: Token $AZION_API_TOKEN" \
   | jq -c '.results[] | select(.name == "api" or .name == "shop")'
   ```

   ```text
   {"id":612522,"name":"shop","ttl":600,"type":"A","rdata":["192.0.2.10","192.0.2.11"]}
   {"id":612523,"name":"api","ttl":600,"type":"A","rdata":["192.0.2.20"]}
   ```

2. Delete the records that the list shows. Replace `[TOKEN VALUE]` and `<your-zone-id>` with your own values:

   ```bash
   export AZION_API_TOKEN="[TOKEN VALUE]"
   ZONE_ID=<your-zone-id>
   curl --silent "https://api.azion.com/v4/workspace/dns/zones/$ZONE_ID/records?page_size=100&fields=id,name" \
     --header "Authorization: Token $AZION_API_TOKEN" \
   | jq -r '.results[] | select(.name == "api" or .name == "shop") | .id' \
   | while read -r record_id; do
     curl --silent --request DELETE "https://api.azion.com/v4/workspace/dns/zones/$ZONE_ID/records/$record_id" \
       --header "Authorization: Token $AZION_API_TOKEN"
     echo " $record_id"
   done
   ```

   ```text
   {"state":"executed"} 612522
   {"state":"executed"} 612523
   ```

Check the records that remain in the zone. Replace `[TOKEN VALUE]` and `<your-zone-id>` with your own values:

```bash
export AZION_API_TOKEN="[TOKEN VALUE]"
ZONE_ID=<your-zone-id>
curl --silent "https://api.azion.com/v4/workspace/dns/zones/$ZONE_ID/records?page_size=100&fields=id,name,type,rdata,ttl" \
  --header "Authorization: Token $AZION_API_TOKEN" \
| jq -c '.results[]'
```

```text
{"id":612521,"name":"www","ttl":3600,"type":"CNAME","rdata":["example.org"]}
{"id":612524,"name":"@","ttl":3600,"type":"MX","rdata":["10 mail.example.org"]}
{"id":612525,"name":"@","ttl":3600,"type":"TXT","rdata":["v=spf1 -all"]}
```

The zone no longer holds the records named `api` and `shop`.

---

## Next steps

- [Troubleshoot Azion API](/en/documentation/devtools/api/troubleshooting.md): Causes and fixes for the 400, 401, 404, and 405 responses of the API v4.
- [Query a zone with dig](/en/documentation/guides/application-security/dns/run-the-dig-command.md): Check what Azion's nameservers answer for the records you changed.
