To stop an in-progress CSV or SQL import and free up cluster resources, you can
cancel the AlloyDB for PostgreSQL import operation using the Google Cloud CLI or the
REST API.

## Before you begin

To cancel the import of data, find the ID of the import operation that you want
to cancel. To find the ID, do one of the following:

- Follow the instructions in [Import a CSV file](https://docs.cloud.google.com/alloydb/docs/import-csv-file.md.txt) or [Import a SQL file](https://docs.cloud.google.com/alloydb/docs/import-sql-file.md.txt). The operation ID is returned in the `name` field of the response.
- Make an [`operations.list`](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/operations/list.md.txt) call on the AlloyDB cluster.

## Cancel the import operation

You can use the Google Cloud CLI or REST API commands to cancel an import
operation.

### gcloud

Use the [`gcloud alloydb operations cancel`](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/operations/cancel.md.txt)
command to cancel the operation:

```bash
gcloud alloydb operations cancel OPERATION_ID --region=REGION
```

Replace the following:

- `OPERATION_ID`: the ID of the operation. For more information, see [Before you begin](https://docs.cloud.google.com/alloydb/docs/cancel-import.md.txt#before-you-begin).
- `REGION`: the region in which the AlloyDB cluster is deployed.

### REST v1

Use the following HTTP method and URL:

```bash
POST https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID:cancel
```

For more information, see [`operations.cancel`](https://docs.cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.operations/cancel.md.txt).

Before you use any of the request data, make the following replacements:

- <var translate="no">PROJECT_ID</var>: the project ID.
- <var translate="no">REGION</var>: the region in which the AlloyDB cluster is deployed.
- <var translate="no">OPERATION_ID</var>: the ID of the import operation.
  For more information, see [Before you begin](https://docs.cloud.google.com/alloydb/docs/cancel-import.md.txt#before-you-begin).

  To send your request, expand one of these options:

  #### curl (Linux, macOS, or Cloud Shell)

  The following command assumes that you signed into the Google Cloud CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt), or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically signs you into the gcloud CLI. You can check the active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).

  Save the request body in a file named `request.json`
  and execute the following command:

  ```
     curl -X POST \
           -H "Authorization: Bearer $(gcloud auth print-access-token)" \
           -H "Content-Type: application/json; charset=utf-8" \
           -d @request.json \
           "https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID:cancel"
  ```

  #### PowerShell (Windows)

  The following command assumes that you signed into the Google Cloud CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt), or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically signs you into the gcloud CLI. You can check the active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).

  Save the request body in a file named `request.json`
  and execute the following command:

  ```
  $cred = gcloud auth print-access-token
  $headers = @{ "Authorization" = "Bearer $cred" }

  Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID:cancel"| Select-Object -Expand Content
  ```

  This REST API call doesn't return any response. For more information
  about checking the cancellation status of the import, see
  [Check the cancelled status](https://docs.cloud.google.com/alloydb/docs/cancel-import.md.txt#check-cancelled-status).

## Check the cancelled status

You can use the gcloud CLI or REST API commands to check the status of a cancelled
import operation.

### gcloud

Use the [`gcloud alloydb operations describe`](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/operations/describe.md.txt)
command to check the status of the cancelled operation.

```bash
gcloud alloydb operations describe OPERATION_ID --region=REGION
```

Replace the following:

- `OPERATION_ID`: the ID of the cancelled operation. For more information, see [Before you begin](https://docs.cloud.google.com/alloydb/docs/cancel-import.md.txt#before-you-begin).
- `REGION`: the region in which the AlloyDB cluster is deployed.

### REST v1

Run the following:

```
GET https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID
```

For more information, see [`operations.get`](https://docs.cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.operations/get.md.txt).

Before you use any of the request data, make the following replacements:

- <var translate="no">REGION</var>: the region in which the AlloyDB cluster is deployed.
- <var translate="no">PROJECT_ID</var>: the project ID.
- <var translate="no">OPERATION_ID</var>: the ID of the import operation. For more information, see [Before you begin](https://docs.cloud.google.com/alloydb/docs/cancel-import.md.txt#before-you-begin).

To send your request, expand one of these options:

#### *curl (Linux, macOS, or Cloud Shell)*

The following command assumes that you signed into the gcloud CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt), or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically signs you into the gcloud CLI. You can check the active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).

Execute the following command:

```
  curl -X GET \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       "https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID"
 
```

#### *PowerShell (Windows)*

The following command assumes that you signed into the gcloud CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt). You can check the active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).

Execute the following command:

```
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
  -Method GET `
  -Headers $headers `
  -Uri "https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID" | Select-Object -Expand Content
```

<br />


If successful, the response body contains an instance of
[`Operation`](https://docs.cloud.google.com/alloydb/docs/reference/rest/Shared.Types/ListOperationsResponse.md.txt#Operation).

<br />

## What's next

- Learn how to [import a CSV file](https://docs.cloud.google.com/alloydb/docs/import-csv-file.md.txt).
- Learn how to [import a SQL file](https://docs.cloud.google.com/alloydb/docs/import-sql-file.md.txt).