Skip to content

Delete a merged pull request's head branch automatically - #15

Merged
arijitchhatui merged 1 commit into
mainfrom
claude/git-actions-branch-deletion-d4acd1
Aug 21, 2026
Merged

Delete a merged pull request's head branch automatically#15
arijitchhatui merged 1 commit into
mainfrom
claude/git-actions-branch-deletion-d4acd1

Conversation

@arijitchhatui

Copy link
Copy Markdown
Collaborator

The head branch of a merged pull request has no readers left: its commits are on main and its name
is preserved in the pull request itself. Left alone they accumulate, and the branch list stops being
a list of live work.

Adds .github/workflows/delete-merged-branch.yml, which fires on pull_request: closed and deletes
the head branch. The guards are the point:

  • Merged only. A pull request closed without merging keeps its branch, because the work on it
    has not landed anywhere.
  • Same repository only. A fork's branch is not ours to delete.
  • Never the default branch, and never main, master, develop or release/* — long-lived
    branches merge into main like any other, so the default-branch check alone does not cover them.
  • Already deleted is a no-op, not a failure, so deleting from the merge screen or re-running the
    workflow both exit cleanly.

It uses the built-in GITHUB_TOKEN with contents: write — no personal access token, and no
third-party action.

GitHub's "Automatically delete head branches" repository setting does the same job, but it is a
checkbox nobody can see from a clone; this keeps the behaviour in the tree where it is reviewable.
The two are safe to run together — whichever gets there first wins and the other exits 0.

The head branch of a merged pull request has no readers left: its commits are on main and its name
is preserved in the pull request itself. Left alone they accumulate, and the branch list stops being
a list of live work.

Adds `.github/workflows/delete-merged-branch.yml`, which fires on `pull_request: closed` and deletes
the head branch. The guards are the point:

- **Merged only.** A pull request closed without merging keeps its branch, because the work on it
  has not landed anywhere.
- **Same repository only.** A fork's branch is not ours to delete.
- **Never the default branch**, and never `main`, `master`, `develop` or `release/*` — long-lived
  branches merge into main like any other, so the default-branch check alone does not cover them.
- **Already deleted is a no-op**, not a failure, so deleting from the merge screen or re-running the
  workflow both exit cleanly.

It uses the built-in `GITHUB_TOKEN` with `contents: write` — no personal access token, and no
third-party action.

GitHub's "Automatically delete head branches" repository setting does the same job, but it is a
checkbox nobody can see from a clone; this keeps the behaviour in the tree where it is reviewable.
The two are safe to run together — whichever gets there first wins and the other exits 0.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Aug 21, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
uploadflow Ready Ready Preview Aug 21, 2026 5:49am

@arijitchhatui
arijitchhatui merged commit 5c3ec88 into main Aug 21, 2026
2 checks passed
@github-actions
github-actions Bot deleted the claude/git-actions-branch-deletion-d4acd1 branch August 21, 2026 05:52
arijitchhatui added a commit that referenced this pull request Aug 21, 2026
The "Automatically delete head branches" repository setting has been turned on, which does the same
job as the workflow added in #15 — so the workflow is now redundant.

The setting wins on what matters here: it cannot fail, because there is no runner, no token and no
job to queue, and it costs no Actions minutes on every merge. The workflow's one advantage was being
visible in the tree, and that is worth less than a mechanism with nothing to break.

Removes `.github/workflows/delete-merged-branch.yml`. Behaviour is unchanged — merged head branches
are still deleted automatically, and the setting leaves fork branches and closed-without-merge pull
requests alone on its own.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant