# gh pr close: Close a Pull Request from CI

> gh pr close closes a pull request and can delete its branch or add a comment. Reference for --delete-branch, --comment, and the permission errors in CI.

Source: https://latchkey.dev/learn/command-reference/gh-pr-close  
Updated: 2026-06-30

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@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

### 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.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
