---
type: api
title: Cancel a task
description: **All methods and paths for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/operation/operation-tasks-cancel
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/operation/operation-tasks-cancel
products:
  - Elasticsearch
---

# Cancel a task


Availability: 2.3+

## Paths

- `POST` `/_tasks/{task_id}/_cancel`
- `POST` `/_tasks/_cancel`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/authentication#basicauth)
- [Bearer auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/authentication#bearerauth)

#### Path Parameters

- `task_id` (string TaskId) — optional, leave it out to use `/_tasks/_cancel`
  The task identifier.

## Description

WARNING: The task management API is new and should still be considered a beta feature.
The API may change in ways that are not backwards compatible.

A task may continue to run for some time after it has been cancelled because it may not be able to safely stop its current activity straight away.
It is also possible that Elasticsearch must complete its work on other tasks before it can process the cancellation.
The get task information API will continue to list these cancelled tasks until they complete.
The cancelled flag in the response indicates that the cancellation command has been processed and the task will stop as soon as possible.

To troubleshoot why a cancelled task does not complete promptly, use the get task information API with the `?detailed` parameter to identify the other tasks the system is running.
You can also use the node hot threads API to obtain detailed information about the work the system is doing instead of completing the cancelled task.

For relocatable tasks, this API transparently follows the task across graceful shutdown relocations,
so callers can keep using the original task ID. The returned task reports its `original_task_id` and `original_start_time_in_millis`
if it is continuing work from an earlier task.

## Required authorization

* Cluster privileges: `manage`

## Query String Parameters

- `actions` (union string | [] string)
  A comma-separated list or wildcard expression of actions that is used to limit the request.
  One of: `string` or `string[]`
- `nodes` ([] string)
  A comma-separated list of node IDs or names that is used to limit the request.
- `parent_task_id` (string)
  A parent task ID that is used to limit the tasks.
- `wait_for_completion` (boolean · default: false)
  If true, the request blocks until all found tasks are complete.

## Response

`application/json`

- `node_failures` ([] ErrorCause)
  - `type` (string)
    The type of error
  - `reason` (union string) — nullable
    A human-readable explanation of the error, in English.
    One of: `string`
  - `stack_trace` (string)
    The server stack trace. Present only if the `error_trace=true` parameter was sent with the request.
  - `caused_by` ({} ErrorCause) — recursive
    Same fields as `node_failures`
  - `root_cause` ([] ErrorCause) — recursive
    Same fields as `node_failures`
  - `suppressed` ([] ErrorCause) — recursive
    Same fields as `node_failures`
- `task_failures` ([] TaskFailure)
  - `task_id` (number)
  - `node_id` (string)
  - `status` (string)
  - `reason` ({} ErrorCause)
    - `type` (string)
      The type of error
    - `reason` (union string) — nullable
      A human-readable explanation of the error, in English.
      One of: `string`
    - `stack_trace` (string)
      The server stack trace. Present only if the `error_trace=true` parameter was sent with the request.
    - `caused_by` ({} ErrorCause) — recursive
      Same fields as `reason` in `task_failures`
    - `root_cause` ([] ErrorCause) — recursive
      Same fields as `reason` in `task_failures`
    - `suppressed` ([] ErrorCause) — recursive
      Same fields as `reason` in `task_failures`
- `nodes` (map string to NodeTasks)
  Task information grouped by node, if `group_by` was set to `node` (the default).
  Map values ({} NodeTasks)
  - `name` (string)
  - `transport_address` (string)
  - `host` (string)
  - `ip` (string)
  - `roles` ([] string)
  - `attributes` (map string to string)
  - `tasks` (map string to TaskInfo)
    Map values ({} TaskInfo)
    - `action` (string)
    - `cancelled` (boolean)
    - `cancellable` (boolean)
    - `description` (string)
      Human readable text that identifies the particular request that the task is performing.
For example, it might identify the search request being performed by a search task.
Other kinds of tasks have different descriptions, like `_reindex` which has the source and the destination, or `_bulk` which just has the number of requests and the destination indices.
Many requests will have only an empty description because more detailed information about the request is not easily available or particularly helpful in identifying the request.
    - `headers` (map string to string)
    - `id` (number)
    - `node` (string)
    - `running_time` (string Duration)
    - `running_time_in_nanos` (DurationValueUnitNanos)
    - `start_time_in_millis` (EpochTimeUnitMillis)
    - `status` (object)
      The internal status of the task, which varies from task to task.
The format also varies.
While the goal is to keep the status for a particular task consistent from version to version, this is not always possible because sometimes the implementation changes.
Fields might be removed from the status for a particular request so any parsing you do of the status might break in minor releases.
    - `type` (string)
    - `parent_task_id` (string TaskId)
    - `original_task_id` (string TaskId)
      The task ID of the original task. Only present when the task is continuing the work of an earlier task that was running on a node which has since shut down (i.e. a relocatable task).
For tasks that have not been relocated this is always equal to the task's own ID and is omitted from the response.
    - `original_start_time_in_millis` (EpochTimeUnitMillis)
      The time at which the original task started, in milliseconds since the Unix epoch. Only present together with `original_task_id`.
    - `original_start_time` (string)
      The time at which the original task started, as an ISO 8601 formatted string. Only present together with `original_task_id` and when the request includes the `?human=true` query parameter.
- `tasks` (TaskInfos)
  Either a flat list of tasks if `group_by` was set to `none`, or grouped by parents if
`group_by` was set to `parents`.
## Code Examples

#### Console

```console
POST _tasks/<task_id>/_cancel
```

#### JavaScript

```javascript
const response = await client.tasks.cancel({
  task_id: "<task_id>",
});
```

#### Python

```python
resp = client.tasks.cancel(
    task_id="<task_id>",
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_tasks/<task_id>/_cancel" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY"
```

#### Java

```java
client.tasks().cancel(c -> c
    .taskId("<task_id>")
);
```

#### PHP

```php
$resp = $client->tasks()->cancel([
    "task_id" => "<task_id>",
]);
```

#### Ruby

```ruby
response = client.tasks.cancel(
  task_id: "<task_id>"
)
```

