# Cancel workflow job

```{raw} html
<div class="mo-api-page-show-toc" aria-hidden="true"></div>
```

Requests cancellation of a specified workflow job. HTTP success does not mean that the job has been terminated; please read the return status or query the job details to confirm the final status.

```text
POST https://moi.matrixorigin.cn/newmoi/workflow/v2/workflow-apps/{workflow_id}/executions/{execution_id}/cancel
```

## Preparation before calling

First [query job details](get-workflow-job.md) and check whether `available_actions` contains cancellation. Prepare a personal access token and target workspace ID that has access to the target workspace.

The request body cannot contain `runtime_context`.

## Request body

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>workflow_id</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>Workflow ID.</dd></div>
  <div class="mo-api-field"><dt><code>execution_id</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>Job ID.</dd></div>
  <div class="mo-api-field"><dt><code>reason</code><span class="mo-api-field__type">string</span></dt><dd>Compatible field; currently does not affect cancellation processing.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Request example</p>
```

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/workflow/v2/workflow-apps/$WORKFLOW_ID/executions/$EXECUTION_ID/cancel" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d '{
    "reason": "<REASON>"
  }'
```

:::
::::

## Successful response

Returns `200` when the service accepts the cancellation request and returns the job details at that time. The returned status can still be the current running status; query the job details to confirm the final cancellation status.

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">string</span></dt><dd>`OK` when successful.</dd></div>
  <div class="mo-api-field"><dt><code>msg</code><span class="mo-api-field__type">string</span></dt><dd>`OK` when successful.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.execution_id</code><span class="mo-api-field__type">string</span></dt><dd>Execution ID.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.workflow_id</code><span class="mo-api-field__type">string</span></dt><dd>Workflow ID.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.status</code><span class="mo-api-field__type">string</span></dt><dd>Execution status when the cancellation request is accepted; this response alone does not confirm final cancellation.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.available_actions</code><span class="mo-api-field__type">string[]</span></dt><dd>Currently allowed operations.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.execution_mode</code><span class="mo-api-field__type">string</span></dt><dd>Execution mode.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.cron_expression</code><span class="mo-api-field__type">string</span></dt><dd>Cron expression; only returned in timing mode.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.input_payload</code><span class="mo-api-field__type">object</span></dt><dd>Expanded run input.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.vars_payload</code><span class="mo-api-field__type">object</span></dt><dd>Expanded run variable.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.moi_task_id</code><span class="mo-api-field__type">string</span></dt><dd>Task ID; returned when the server has assigned it.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.moi_case_id</code><span class="mo-api-field__type">string</span></dt><dd>Case ID; returned when the server has assigned it.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.moi_workflow_def_id</code><span class="mo-api-field__type">string</span></dt><dd>Workflow definition identifier; returned when the server has assigned it.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.moi_workflow_version_id</code><span class="mo-api-field__type">string</span></dt><dd>Workflow version identifier; returned when the server has assigned it.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.pause_scope</code><span class="mo-api-field__type">string</span></dt><dd>Pause range; returned when there is a value.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.error</code><span class="mo-api-field__type">string</span></dt><dd>Error message; returned if there is a value.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.started_at</code><span class="mo-api-field__type">string</span></dt><dd>Start time; may be omitted if not generated.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.ended_at</code><span class="mo-api-field__type">string</span></dt><dd>End time; may be omitted if not generated.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.created_at</code><span class="mo-api-field__type">string</span></dt><dd>Creation time; returned when provided by the server.</dd></div>
  <div class="mo-api-field"><dt><code>data.execution.updated_at</code><span class="mo-api-field__type">string</span></dt><dd>Last update time; returned when provided by the server.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Successful response example</p>
```

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "execution": {
      "execution_id": "exec-001",
      "workflow_id": "wf-001",
      "status": "running",
      "available_actions": [],
      "execution_mode": "one_shot",
      "input_payload": {},
      "vars_payload": {}
    }
  }
}
```

:::
::::

## Error response

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">string</span></dt><dd>Error code.</dd></div>
  <div class="mo-api-field"><dt><code>msg</code><span class="mo-api-field__type">string</span></dt><dd>Readable error message.</dd></div>
  <div class="mo-api-field"><dt><code>data</code><span class="mo-api-field__type">null</span></dt><dd>`null` in an error response.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Error response example</p>
```

```json
{
  "code": "ErrParamInvalid",
  "msg": "请求参数无效",
  "data": null
}
```

:::
::::

## Follow-up operations

Use the same job ID [query workflow job details](get-workflow-job.md) to confirm the final status.
