From 2b1eaa302bc233b3377fae2bd902375ce9308c5c Mon Sep 17 00:00:00 2001 From: Michal Ferber Date: Sat, 22 Aug 2026 23:43:51 +0000 Subject: [PATCH] ci: auto-delete merged PR head branches Actions workflow standing in for the "Automatically delete head branches" repo setting: on a merged pull_request it deletes the head ref, guarded against fork branches and the default branch, and tolerant of a ref that is already gone (so it coexists with the native setting if that is ever enabled). Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_017V8qVCQEZFKAkKTByQrMZk --- .github/workflows/delete-merged-branch.yml | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/delete-merged-branch.yml diff --git a/.github/workflows/delete-merged-branch.yml b/.github/workflows/delete-merged-branch.yml new file mode 100644 index 0000000..b00df87 --- /dev/null +++ b/.github/workflows/delete-merged-branch.yml @@ -0,0 +1,35 @@ +# Deletes a PR's head branch once the PR is merged — the Actions equivalent of +# the repo setting "Automatically delete head branches" (Settings → General → +# Pull Requests). If that setting is ever enabled too, the two coexist: an +# already-deleted ref is tolerated below. + +name: delete-merged-branch + +on: + pull_request: + types: [closed] + +permissions: + contents: write + +jobs: + delete: + # Merged PRs only; never fork branches, never the default branch. + if: >- + github.event.pull_request.merged == true && + github.event.pull_request.head.repo.full_name == github.repository && + github.event.pull_request.head.ref != github.event.repository.default_branch + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Delete merged head branch + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + BRANCH: ${{ github.event.pull_request.head.ref }} + run: | + if gh api -X DELETE "repos/$REPO/git/refs/heads/$BRANCH"; then + echo "Deleted branch: $BRANCH" + else + echo "Branch $BRANCH is already gone — nothing to delete." + fi