# Manage named sets

This guide describes how to do the following updates to named sets for
Cloud Router BGP route policies:

- List named sets
- Remove named sets
- Update named sets
- Add named set elements
- Remove named set elements

## List named sets on a router

Use the [`list-named-sets`](https://docs.cloud.google.com/sdk/gcloud/reference/compute/routers/list-named-sets) command to view the names and types of named sets
defined on a single router.

```
gcloud compute routers list-named-sets ROUTER_NAME
```

Replace <var translate="no">ROUTER_NAME</var> with the name of your Cloud Router.

## Remove a named set

If a named set is referenced in any BGP route policies on the router, you can't remove the named set.

```
gcloud compute routers remove-named-set ROUTER_NAME
    --set-name=SET_NAME
```

Replace the following:

- <var translate="no">ROUTER_NAME</var>: The name of your Cloud Router
- <var translate="no">SET_NAME</var>: The name of the named set that you want to remove

## Update a named set on a router

When updating an existing named set, we recommend that you download the named set
from the router in either YAML or JSON format by using the
[`download-named-set`](https://docs.cloud.google.com/sdk/gcloud/reference/compute/routers/download-named-set)
command. Edit the YAML or JSON file locally, and then upload the updated
file to the router by using the [`upload-named-set`](https://docs.cloud.google.com/sdk/gcloud/reference/compute/routers/upload-named-set)
command. The `upload-named-set` command overwrites the existing named set.

The file that you download from the router includes the `fingerprint` field,
which Cloud Router uses to match
the existing named set and prevent conflicting updates.

1. To download the named set, run the `download-named-set` command:

   ```
   gcloud compute routers download-named-set ROUTER_NAME
       --set-name=SET_NAME
       --file-name=FILE_NAME
       --file-format=FORMAT
   ```

   <br />

   Replace the following:
   - <var translate="no">ROUTER_NAME</var> with the name of the Cloud Router.
   - <var translate="no">SET_NAME</var> with the name of the set.
   - <var translate="no">FILE_NAME</var> with the filename you want for the named set, including the path and filename extension.
   - <var translate="no">FORMAT</var> with the format of the named set, either JSON or YAML.
2. Modify the downloaded named set in your preferred text editor using either
   JSON or YAML formatting.

   The following examples use two YAML files to update named sets named
   `allowed-prefixes` and `allowed-communities`.

   **allowed-prefixes.yaml:**

       # Prefix set
       resource:
         elements:
         - expression: "'192.168.1.0/24'"
         - expression: "'fd00::/8'"
         - expression: "prefix('10.0.0.0/20').orLonger()"
         name: allowed-prefixes
         type: NAMED_SET_TYPE_PREFIX

   **allowed-communities.yaml:**

       # Community set
       resource:
         elements:
         - expression: "'64500:100'"
         - expression: "'64500:300'"
         name: allowed-communities
         type: NAMED_SET_TYPE_COMMUNITY

3. To upload the named set, run the `upload-named-set` command:

   ```
   gcloud compute routers upload-named-set ROUTER_NAME
       --set-name=SET
       --file-name=NAME
       --file-format=FORMAT
   ```

   <br />

   Replace the following:
   - <var translate="no">ROUTER_NAME</var> with the name of the Cloud Router.
   - <var translate="no">SET</var> with the name of the set.
   - <var translate="no">NAME</var> with the location and filename of the YAML or JSON file you edited.
   - <var translate="no">FORMAT</var> with the format of the named set, either JSON or YAML.

## Add a named set element

Run the following command:

```
gcloud compute routers add-named-set-element ROUTER_NAME
    --set-name=SET_NAME
    --new-set-element=CEL_EXPRESSION
```

Replace the following:

- <var translate="no">ROUTER_NAME</var>: The name of your Cloud Router
- <var translate="no">SET_NAME</var>: The name of the named set that you want to update
- <var translate="no">CEL_EXPRESSION</var>: An element to add to the set, expressed in Common Expression Language (CEL)

## Remove a named set element

Run the following command:

```
gcloud compute routers remove-named-set-element ROUTER_NAME
    --set-name=SET_NAME
    --set-element=CEL_EXPRESSION
```

Replace the following:

- <var translate="no">ROUTER_NAME</var>: The name of your Cloud Router
- <var translate="no">SET_NAME</var>: The name of the named set that you want to update
- <var translate="no">CEL_EXPRESSION</var>: An element to remove from the set, expressed in Common Expression Language (CEL)

## What's next

- [Create BGP route policies](https://docs.cloud.google.com/network-connectivity/docs/router/how-to/bgp-route-policies/create-policies)
- [Update BGP route policies](https://docs.cloud.google.com/network-connectivity/docs/router/how-to/bgp-route-policies/update-policies)