# Cancel data subscription

Cancel an accepted data subscription. Upon success, the subscription database in the target workspace is deleted and the subscription record is restored to a reacceptable invitation.

```text
POST https://moi.matrixorigin.cn/newmoi/data-share/subscriptions/{id}/unsubscribe
```

## Preparation before calling

First [query the data subscription list](list-subscriptions.md) and confirm the subscription to be canceled. Prepare the personal access token, target workspace ID, and subscription ID that have access to the target workspace.

The example below uses:

- `$AI_STUDIO_API_KEY`: The actual personal access token, passed through the `X-API-Key` Header.
- `$WORKSPACE_ID`: Target workspace ID, passed through `X-Workspace-ID` Header.
- `$SUBSCRIPTION_ID`: The subscription ID to be canceled, taken from the response of [query data subscription list](list-subscriptions.md).

## Request example

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/data-share/subscriptions/$SUBSCRIPTION_ID/unsubscribe" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID"
```

## Path parameters

| Parameters | Type | Description |
| --- | --- | --- |
| `id` | integer | A subscription ID greater than `0`. |

## Successful response

Returns `200` on success, `data` is `null`.

```json
{
  "code": "OK",
  "msg": "OK",
  "data": null
}
```

The response fields are as follows.

| Field | Type | Description |
| --- | --- | --- |
| `code` | string | `OK` on success. |
| `msg` | string | `OK` on success. |
| `data` | null | Fixed to `null` when the cancellation is successful. |

## Error response

```json
{
  "code": "ErrSubscriptionNotFound",
  "msg": "数据订阅不存在",
  "data": null
}
```

### Common HTTP errors

```{list-table}
:header-rows: 1
:widths: 12 22 32 34

* - HTTP status code
  - error code
  - Common causes
  - Recommended actions
* - `400`
  - `ErrParamInvalid`
  - `id` is not an integer greater than `0`.
  - Use the ID from the subscription list.
* - `403`
  - `ErrPermissionDenied`
  - The current identity does not have the permission to delete subscriptions.
  - Request permission to delete data subscriptions.
* - `404`
  - `ErrSubscriptionNotFound`
  - The subscription does not exist or does not belong to the current workspace.
  - Verify subscription ID and workspace.
* - `409`
  - `ErrSubscriptionDeleteConflict`
  - The subscription status has changed and cannot be canceled at this time.
  - Refresh the subscription list and try again.
* - `503`
  - `ErrDataShareWritesPaused`
  - Data sharing and writing are temporarily under maintenance or migration access.
  - Try again later.
* - `500`
  - `ErrServer`
  - The server failed to cancel the subscription.
  - Try again later.
```

## Follow-up operations

[Query data subscription list](list-subscriptions.md), confirm that the status of the record has been restored to a re-acceptable subscription invitation.
