Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/delete-merged-branch.yml
Original file line number Diff line number Diff line change
@@ -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