# Deleting a NAT gateway

If you no longer need the [NAT gateway](../concepts/gateways.md), you can [delete it](#delete-nat-gateway). Before deleting your NAT gateway, [disassociate it](#unlink-nat-gateway) from all route tables that use it.

## Disassociating a NAT gateway from a route table {#unlink-nat-gateway}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder containing the NAT gateway you want to dissociate.
  1. Navigate to **Virtual Private Cloud**.
  1. In the left-hand panel, select ![image](../../_assets/console-icons/route.svg) **Routing tables**.
  1. In the list that opens, find the route table with the NAT gateway listed in its **Static routes** column.
  1. In the table row, click ![image](../../_assets/console-icons/ellipsis.svg) and select **Edit**.
  1. In the window that opens, click ![image](../../_assets/console-icons/xmark.svg) in the row with the NAT gateway name.
  1. Click **Save**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To disassociate a NAT gateway from a route table:

  1. View the description of the CLI command for updating a route table:

      ```bash
      yc vpc route-table update --help
      ```

  1. Get the name or ID of the route table the NAT gateway is associated with:

      ```bash
      yc vpc route-table list
      ```
      Result:
      ```text
      +----------------------+----------------------+-------------+----------------------+
      |          ID          |         NAME         | DESCRIPTION |      NETWORK-ID      |
      +----------------------+----------------------+-------------+----------------------+
      | enpcaaqahk3c******** | test-table           |             | enptgj64mv2r******** |
      +----------------------+----------------------+-------------+----------------------+

      ```
  1. Disassociate the NAT gateway:
      ```bash
      yc vpc route-table update <route_table_name_or_ID> --clear-routes
      ```
      Use either the `--id` or `--name` parameter.

- Terraform {#tf}

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the relevant documentation on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).

  To disassociate a NAT gateway from a route table created using Terraform:

  1. Open the Terraform configuration file and find the fragment with the description of the route table the NAT gateway is associated with:

     {% cut "Example of a table description in the Terraform configuration" %}

     ```hcl
     ...
     resource "yandex_vpc_route_table" "routetable" {
       folder_id  = "<user_folder_ID>"
       name       = "<route_table_name>"
       network_id = "<network_ID>"
       static_route {
         destination_prefix = "0.0.0.0/0"
         gateway_id         = <gateway_ID>
       }
     }
     ...
     ```

     {% endcut %}

  1. Delete the `static_route` object.

  1. In the command line, go to the directory with the Terraform configuration file.

  1. Check the configuration using this command:

     ```bash
     terraform validate
     ```

     If the configuration is valid, you will get this message:

     ```text
     Success! The configuration is valid.
     ```

  1. Run this command:

     ```bash
     terraform plan
     ```

     You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.

  1. Apply the configuration changes:

     ```bash
     terraform apply
     ```

  1. Type `yes` and press **Enter** to confirm changes.

     You can check the updates using the [management console](https://console.yandex.cloud) or this [CLI](../../cli/quickstart.md) command:

     ```bash
     yc vpc route-table get <route_table_name_or_ID>
     ```
     Use either the `--id` or `--name` parameter.

- API {#api}

  To disassociate a NAT gateway from a route table, delete the static route with that NAT gateway from the route table. To do this, use the [update](../api-ref/RouteTable/update.md) REST API method for the [RouteTable](../api-ref/RouteTable/index.md) resource or the [RouteTableService/Update](../api-ref/grpc/RouteTable/update.md) gRPC API call. In your request, provide a list of static routes without the route you want to delete under:
    * `staticRoutes`: For REST API
    * `static_routes`: For gRPC API

  If the list contained a single static route, provide an empty list.

  To get a list of static routes, use the [get](../api-ref/RouteTable/get.md) REST API method for the [RouteTable](../api-ref/RouteTable/index.md) resource or the [RouteTableService/Get](../api-ref/grpc/RouteTable/get.md) gRPC API call. In your request, provide the route table ID in this parameter:
    * `routeTableId`: For REST API
    * `route_table_id`: For gRPC API

  {% note warning %}
  
  The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
  
  {% endnote %}

{% endlist %}

## Deleting a NAT gateway {#delete-nat-gateway}

{% note warning %}

Before deleting a NAT gateway, [disassociate it](#unlink-route-table) from all route tables it is associated with.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

  To delete a NAT gateway:
  1. In the [management console](https://console.yandex.cloud), select the folder where you need to delete a NAT gateway.
  1. Navigate to **Virtual Private Cloud**.
  1. In the left-hand panel, select ![image](../../_assets/vpc/gateways.svg) **Gateways**.
  1. Click ![image](../../_assets/console-icons/ellipsis.svg) in the row with the NAT gateway name and select **Delete**.
  1. In the window that opens, click **Delete**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To delete a route table:

  1. View the description of the CLI command for deleting a NAT gateway:

      ```bash
      yc vpc gateway delete --help
      ```

  1. Get the name or ID of the NAT gateway to delete:

      ```bash
      yc vpc gateway list
      ```
      Result:
      ```text
      +----------------------+-----------+-------------+
      |          ID          |   NAME    | DESCRIPTION |
      +----------------------+-----------+-------------+
      | enpkq171u4gb******** | gateway-1 |             |
      +----------------------+-----------+-------------+
      ```
  1. Delete the NAT gateway:
      ```bash
      yc vpc gateway delete <gateway_name_or_ID>
      ```
      Use either the `--id` or `--name` parameter.

- Terraform {#tf}

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the relevant documentation on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).

  To delete a NAT gateway created using Terraform:

  1. Open the Terraform configuration file and delete the NAT gateway description fragment.

     {% cut "Example of a NAT gateway description in the Terraform configuration" %}

     ```hcl
     ...
     resource "yandex_vpc_gateway" "natgateway" {
       folder_id      = "<folder_ID>"
       name = "<gateway_name>"
       shared_egress_gateway {}
     }
     ...
     ```

     {% endcut %}

  1. In the command line, navigate to the directory with the Terraform configuration file.

  1. Check the configuration using this command:

     ```bash
     terraform validate
     ```

     If the configuration is valid, you will get this message:

     ```text
     Success! The configuration is valid.
     ```

  1. Run this command:

     ```bash
     terraform plan
     ```

     You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.

  1. Apply the configuration changes:

     ```bash
     terraform apply
     ```

  1. Type `yes` and press **Enter** to confirm changes.

     You can check the updates using the [management console](https://console.yandex.cloud) or this [CLI](../../cli/quickstart.md) command:

     ```bash
     yc vpc gateway list
     ```

- API {#api}

  To delete a NAT gateway, use the [delete](../api-ref/Gateway/delete.md) REST API method for the [Gateway](../api-ref/Gateway/index.md) resource or the [GatewayService/Delete](../api-ref/grpc/Gateway/delete.md) gRPC API call. In your request, provide the ID of the NAT gateway you want to delete in this parameter:
    * `gatewayId`: For REST API
    * `gateway_id`: For gRPC API

  To get the NAT gateway ID, use the [list](../api-ref/Gateway/list.md) REST API method for the [Gateway](../api-ref/Gateway/index.md) resource or the [GatewayService/List](../api-ref/grpc/Gateway/list.md) gRPC API call. In your request, provide the folder ID in this parameter:
    * `folderId`: For REST API
    * `folder_id`: For gRPC API

{% endlist %}