Sub topic 12
Cancelling runs manually
This chapter assumes you know the concurrency key, cancel-in-progress, and the difference between a running run and a pending run from the earlier chapters. You should also be comfortable reading a workflow file and running commands in a terminal.
Concurrency settings cancel runs automatically, but they only cover cases you planned for. Sometimes you need to stop a run yourself: a bad commit is burning runner minutes, a deployment is stuck, or a pending run is about to start something you no longer want. GitHub gives you three ways to do that: the web UI, the GitHub CLI, and the REST API.
When manual cancellation is the right tool
Manual cancellation fits cases that a group name and cancel-in-progress cannot express.
- A run was started by mistake, for example a
workflow_dispatchwith the wrong inputs. - A run is hung and the job timeout is still far away.
- A group has
cancel-in-progress: false(for example a deployment group) and you decide this specific run should stop. - You want to drop a pending run so it does not start when the running one finishes.
- A script or another workflow needs to cancel runs across many branches.
If you cancel the same kind of run by hand every week, change the concurrency configuration instead. Manual cancellation is for exceptions.
Cancelling in the web UI
- Open the repository and select the Actions tab.
- Select the workflow in the left sidebar, then select the run you want to stop.
- Click Cancel workflow at the top right of the run page.
The run first shows a Canceling state while GitHub signals the running jobs. It becomes Cancelled once they have stopped. You need write access to the repository to see the button.
You can also cancel a run that is still queued or waiting. A run that is pending behind another run in the same concurrency group can be cancelled in the same way, and it will then never start.
Cancelling with the GitHub CLI
The CLI command from the earlier example lists in-progress runs and cancels one by ID.
gh run list --workflow ci.yml --status in_progress
gh run cancel <run-id>gh run list prints the run ID in the last column. If you run gh run cancel with no argument in a terminal, the CLI asks you to pick from a list of recent runs.
Useful filters on gh run list:
| Flag | Purpose |
|---|---|
| `--workflow ci.yml` | Only runs of one workflow, by file name or name |
| `--branch my-branch` | Only runs for one branch |
| `--status in_progress` | Only runs in one status. Also accepts `queued`, `waiting`, `pending` and others |
| `--limit 50` | How many runs to return (the default is 20) |
| `--json databaseId --jq '...'` | Machine-readable output for scripts |
To cancel every in-progress run of a workflow on one branch, combine the filters with xargs:
gh run list \
--workflow ci.yml \
--branch feature/login \
--status in_progress \
--json databaseId \
--jq '.[].databaseId' \
| xargs -n1 gh run cancelQueued runs have a separate status, so run the same command again with --status queued to clear them.
Cancelling with the REST API
The CLI is a thin layer over the REST API. The endpoint is a POST with no body.
gh api --method POST repos/OWNER/REPO/actions/runs/RUN_ID/cancelA successful request returns 202 Accepted. The status code means that GitHub accepted the request, not that the run has already stopped. Cancellation is asynchronous, so poll the run if a script needs to wait for the final state:
gh run view RUN_ID --json status,conclusionExpect status to move from in_progress to completed, with conclusion set to cancelled.
If you call the API from plain curl, you need a token that can write to Actions. A fine-grained personal access token needs the Actions: write repository permission. The built-in GITHUB_TOKEN needs permissions: actions: write in the workflow.
Cancelling from another workflow
You can build a small workflow that cancels runs on request. This example takes a run ID as input.
name: Cancel a run
on:
workflow_dispatch:
inputs:
run_id:
description: ID of the run to cancel
required: true
permissions:
actions: write
jobs:
cancel:
runs-on: ubuntu-latest
steps:
- name: Cancel the run
env:
GH_TOKEN: ${{ github.token }}
RUN_ID: ${{ inputs.run_id }}
run: gh run cancel "$RUN_ID" --repo "$GITHUB_REPOSITORY"Two details matter here. The gh CLI is preinstalled on GitHub-hosted runners but reads its token from GH_TOKEN, so you must set it. The input is passed through an environment variable, not written directly into the script with ${{ }}. That keeps untrusted input out of the shell command text.
What happens when you cancel
Cancelling follows the same path whether the request comes from the UI, the CLI, the API, or a cancel-in-progress rule.
- GitHub marks the run as canceling.
- For each job that is still running, GitHub evaluates the
ifconditions of the job. A job with a condition that evaluates to true, such asalways(), is not cancelled. - For jobs that are cancelled, the runner stops the current step. It sends SIGINT first. If the process is still alive about 7.5 seconds later it sends SIGTERM, and about 2.5 seconds after that it sends SIGKILL.
- Steps with
if: ${{ cancelled() }}orif: ${{ always() }}still run, so cleanup is possible. - The run finishes with the conclusion
cancelled.
Your own scripts should handle SIGINT and SIGTERM if they hold resources, such as a lock file or a temporary cloud environment. The window before SIGKILL is only a few seconds.
The next diagram shows the flow, including the pending run in a concurrency group.
Effect on the concurrency group
Cancelling the running run frees its concurrency group. If another run is pending in that group, it starts once the cancelled run finishes. This is the opposite of what some people expect, so keep it in mind when you cancel a deployment by hand. The next queued deployment starts right away, and it may deploy a commit you did not want.
If you want to stop everything in a group, cancel the pending run first, then the running one.
Cancel and force-cancel
Normal cancellation can fail to stop a run. A job with if: always() is not cancelled, and a run that is stuck because the runner stopped responding may stay in the Canceling state. For these cases the API has a second endpoint.
gh api --method POST repos/OWNER/REPO/actions/runs/RUN_ID/force-cancelForce-cancel ends the run without evaluating conditions such as always(). Cleanup steps do not run.
Common errors
HTTP 409or "Cannot cancel a workflow run that is completed". The run has already finished. Check the status withgh run viewbefore you retry.HTTP 403or "Resource not accessible by integration". The token lacks write access to Actions. Addpermissions: actions: writeto the workflow that makes the call.- The run stays in "Canceling". A job has an
always()condition, or a process ignores the signals. Wait for the step timeout, then use force-cancel. - The wrong run was cancelled. Run IDs are per run, not per workflow. Re-running a workflow keeps the same run ID and increases the attempt number, so confirm the ID with
gh run viewfirst. - A workflow cancelled itself. If a cancel script uses the ID of its own run, it stops itself. Use
${{ github.run_id }}deliberately and filter it out when you cancel in bulk.
Summary of the options
| Method | Best for | Needs |
|---|---|---|
| Web UI | One run, quick decisions | Write access to the repository |
| `gh run cancel` | Terminal use and small scripts | `gh` logged in, or `GH_TOKEN` |
| REST API `cancel` | Integrations and custom tooling | Token with Actions write permission |
| REST API `force-cancel` | Stuck runs that normal cancel cannot stop | Same as above |
Exercise
A deployment workflow uses concurrency: { group: deploy-production, cancel-in-progress: false }. A deployment is running and a second one is pending. You cancel the running deployment by hand. What happens to the pending run, and what should you have done first if you wanted to stop both?
Show answer
When the running deployment ends as cancelled, its group is free, so the pending run starts. To stop both, cancel the pending run first, then cancel the running one.
Exercise
You call the cancel endpoint for a run and get 202 Accepted, but gh run view still shows the run as in progress a minute later. A job in the run has if: always(). What are two possible next steps?
Show answer
The 202 only means the request was accepted, and a job with always() is not cancelled by a normal cancel. You can wait for the job to finish or time out, or call the force-cancel endpoint. Force-cancel skips cleanup steps, so check for leftover resources afterwards.