Skip to content

ci: auto-delete PR head branch on close#105

Merged
JeanExtreme002 merged 2 commits into
mainfrom
ci/delete-branch-on-merge
May 13, 2026
Merged

ci: auto-delete PR head branch on close#105
JeanExtreme002 merged 2 commits into
mainfrom
ci/delete-branch-on-merge

Conversation

@JeanExtreme002
Copy link
Copy Markdown
Owner

@JeanExtreme002 JeanExtreme002 commented May 13, 2026

Summary

  • Adds .github/workflows/delete-pr-branch.yml to delete a PR's head branch automatically whenever the PR is closed (merged or abandoned).
  • Skips protected branches (main, gh-pages) and PRs from forks (where we can't delete refs anyway).
  • Treats 404/422 from the GitHub API as already-deleted (no-op), so re-runs and manual deletions don't fail the job.

Motivation: repo currently has deleteBranchOnMerge: false, so merged branches stick around (e.g. jeanextreme002/* and ci/* from recent PRs are still on the remote). This also cleans up branches from PRs that get closed without merging, which the repo-level setting wouldn't cover.

Alternative — and complementary — fix: flip on Settings → General → Automatically delete head branches in the repo UI. The workflow still adds value because (a) it also handles closed-without-merge and (b) it keeps cleanup behavior in version control.

Test plan

  • Merge this PR and confirm the ci/delete-branch-on-merge head branch is deleted automatically by the new workflow run.
  • Open and merge a follow-up no-op PR from a non-protected branch; confirm the head branch is deleted.
  • Open and close without merging a throwaway PR; confirm the head branch is also deleted.
  • Verify a hypothetical PR targeting main from a fork is a no-op (guarded by the head.repo.full_name == github.repository check).

GitHub's repo-level "automatically delete head branches" is off, so
merged branches linger (e.g. jeanextreme002/* and ci/* from recent PRs).
This workflow deletes the head ref when a PR from this repo is merged,
skipping protected branches (main, gh-pages) and PRs from forks.
@github-actions github-actions Bot added the ci label May 13, 2026
Drop the merged==true guard so abandoned PRs also get their head branch
cleaned up. Rename the workflow file/name to match the broader scope.
@JeanExtreme002 JeanExtreme002 changed the title ci: auto-delete PR head branch after merge ci: auto-delete PR head branch on close May 13, 2026
@JeanExtreme002 JeanExtreme002 merged commit 9b21609 into main May 13, 2026
2 checks passed
@github-actions github-actions Bot deleted the ci/delete-branch-on-merge branch May 13, 2026 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant