> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qoder.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cancel a batch

> Cancel a batch that is running or waiting.

`POST /api/v1/forward/batches/{batch_id}/cancel`

Once cancellation is initiated, Forward drains the queue, marks pending tasks as cancelled, and calls CancelSession on every running task.

## Headers

| Header            | Required | Description                                   |
| ----------------- | -------- | --------------------------------------------- |
| `Authorization`   | Yes      | `Bearer <PAT>`                                |
| `Idempotency-Key` | No       | Optional idempotency key for unsafe requests. |

## Path parameters

| Parameter  | Type   | Required | Description |
| ---------- | ------ | -------- | ----------- |
| `batch_id` | string | Yes      | Batch ID.   |

## Example request

```bash theme={null}
curl -s -X POST 'https://api.qoder.com/api/v1/forward/batches/batch_processing001/cancel' \
  -H "Authorization: Bearer $QODER_PAT"
```

## Example response

**HTTP 200 OK**

```json theme={null}
{
  "id": "batch_processing001",
  "object": "batch",
  "status": "cancelling",
  "input_file_id": "file_input002",
  "completion_window": "24h",
  "created_at": "2026-07-06T11:59:02Z",
  "expires_at": "2026-07-07T11:59:02Z",
  "request_counts": {
    "total": 10,
    "pending": 0,
    "running": 3,
    "completed": 7,
    "failed": 0,
    "cancelled": 0,
    "expired": 0
  },
  "usage": null
}
```

The response is a snapshot at cancellation time: if running tasks remain, `status` is the intermediate `cancelling`; otherwise it is `cancelled` directly. Cancelling a batch already in a terminal state returns the current object idempotently.

## Response fields

| Field        | Type   | Description   |
| ------------ | ------ | ------------- |
| Return value | object | Batch object. |

## Cancellation flow

1. Batch already in a terminal state: returns `200` idempotently, no operation performed.
2. CAS transitions the status `validating|queued|processing → cancelling`.
3. Drain the Redis queue and bulk-mark pending tasks as `cancelled`.
4. Call `CancelSession` on each running task.
5. If no running tasks remain, trigger `finalize` immediately; otherwise wait for the last `CompleteTask` to drive `finalize`.

## Error codes

| HTTP | Type                   | Code                      | Trigger                                          |
| ---- | ---------------------- | ------------------------- | ------------------------------------------------ |
| 404  | `not_found_error`      | `batch_not_found`         | Batch does not exist or belongs to another user. |
| 401  | `authentication_error` | `authentication_required` | PAT is invalid or expired.                       |

## Notes

* Cancellation is asynchronous. A `cancelling` response means the request was accepted; poll the batch detail to confirm the terminal state.
* Cancelling a batch already in a terminal state is idempotent and returns `200`.

## Related

<CardGroup cols={2}>
  <Card title="Get a batch" icon="file-text" href="/cloud-agents/api/forward/batches/get" />

  <Card title="Get output file" icon="download" href="/cloud-agents/api/forward/batches/get-output" />
</CardGroup>
