gh pr close closes a pull request without merging, optionally deleting the head branch.
Cleanup jobs close stale or superseded PRs and tidy up their branches with a single gh pr close call.
What it does
gh pr close closes a pull request in the not-merged state. --delete-branch removes the head branch after closing, and --comment leaves a closing comment explaining why.
Common usage
Terminal
gh pr close 123
gh pr close 123 --delete-branch
gh pr close 123 --comment "Superseded by #130"
Flags
Flag
What it does
-d, --delete-branch
Delete the head branch after closing
-c, --comment <text>
Leave a comment when closing
-R, --repo <owner/repo>
Target a specific repository
In CI
Set GH_TOKEN and permissions: { pull-requests: write, contents: write } if you use --delete-branch (deleting a ref needs contents write). The default GITHUB_TOKEN cannot delete a branch that is protected.
Common errors in CI
"GraphQL: Resource not accessible by integration" means the token lacks pull-requests: write. "failed to delete remote branch ...: HTTP 422" usually means the branch is protected or already gone. Closing an already-closed PR reports it is not open rather than erroring.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
.github/workflows/ci.yml
- uses:actions/checkout@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit rev-parse --abbrev-ref HEAD # prints HEAD when detached
Frequently asked questions
gh pr close: Close a Pull Request from CI?
Cleanup jobs close stale or superseded PRs and tidy up their branches with a single gh pr close call.
What it does?
gh pr close closes a pull request in the not-merged state. --delete-branch removes the head branch after closing, and --comment leaves a closing comment explaining why.
In CI?
Set GH_TOKEN and permissions: { pull-requests: write, contents: write } if you use --delete-branch (deleting a ref needs contents write). The default GITHUB_TOKEN cannot delete a branch that is protected.
Common errors in CI?
"GraphQL: Resource not accessible by integration" means the token lacks pull-requests: write. "failed to delete remote branch ...: HTTP 422" usually means the branch is protected or already gone. Closing an already-closed PR reports it is not open rather than erroring.