Repository navigation
ci: auto-delete head branch after merging automation PRs - #63
Merged
Merged
Conversation
GitHub's repo-level delete_branch_on_merge setting is not honored when PRs are auto-merged via GraphQL (gh pr merge --auto), so automation branches like automated/update-managed-tools accumulate as dangling refs after every weekly merge (see cli/cli#9073). Add a pull_request_target workflow that deletes the head branch when an automation PR closes as merged. Scope is limited to same-repo branches with automation prefixes or trusted bot authors; fork branches are skipped and deletion is idempotent.
SilverKnightKMA
added a commit
that referenced
this pull request
Jul 20, 2026
The root cause of lingering branches was NOT that delete_branch_on_merge fails with auto-merge (that was my earlier, incorrect diagnosis). The repo setting works fine for Dependabot PRs, which use unique branch names per version bump. The real issue: update-managed-tools.yml reused a fixed branch name 'automated/update-managed-tools' across every weekly cron run. When that PR was merged and auto-deleted, the next cron push recreated the same branch, so it always appeared to linger. Switch to a run-id-suffixed branch name so each PR maps to a one-shot branch that GitHub's delete_branch_on_merge removes cleanly, matching the Dependabot pattern. Also drop the redundant delete-branch-on-close.yml workflow added in #63 - it is no longer needed now that the actual root cause is fixed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
GitHub's repo-level
delete_branch_on_mergesetting is not honored when PRs are auto-merged via GraphQL (gh pr merge --auto). As a result, automation branches likeautomated/update-managed-toolsaccumulate as dangling refs after every weekly merge (see cli/cli#9073).This is why the branch keeps showing up in
git log --graphafter PRs like #62 are merged.What
Add
.github/workflows/delete-branch-on-close.yml— apull_request_targetworkflow that deletes the head branch when an automation PR closes as merged.Scope (safe by design):
automated/*,dependabot/*,deps/*,chore/*) or authored by trusted automation bots (dependabot[bot],app/dependabot,github-actions[bot]).Verification
origin/automated/update-managed-toolsdangling until deleted via REST API).