GitHub: the branch is not deleted after merging
Deleting a pull request's head branch after the merge is a separate step, done by the "Automatically delete head branches" setting, the "Delete branch" button on the pull request, or a tool calling the API, and the branch's rules apply to it. A ruleset that restricts deletions lets only users with bypass permissions delete the branches whose name matches its pattern, and this rule is selected by default; a protected branch cannot be deleted unless its protection allows deletions (GitHub's available rules for rulesets and about protected branches, checked October 26, 2026).
DELETE /repos/OWNER/REPO/git/refs/heads/BRANCH 204 No Content | 409 Conflict | 422 Validation failed
What keeps a branch
- Restrict deletions (rulesets): through the REST API it is the rule type
deletion, "Only allow users with bypass permissions to delete matching refs" (GitHub's REST API endpoints for rules). Because it is selected by default, a ruleset made for other rules on a pattern that matches pull request branches keeps those branches too. - A protected branch: by default you cannot delete a protected branch. With "Allow deletions", anyone with at least write permissions to the repository can delete it. A locked branch ("Lock branch") cannot be deleted either.
- Automatic deletion: anyone with admin permissions can turn "Automatically delete head branches" on or off, and branch protection rules and repository rules can also prevent branches being deleted automatically. For a pull request from a fork, GitHub uses the fork's setting, and the person merging needs write access to the fork; otherwise the branch is not deleted automatically (GitHub's managing the automatic deletion of branches).
- The same setting through the API:
delete_branch_on_mergeinPATCH /repos/OWNER/REPOturns automatic deletion of head branches on (true) or off; it isfalseby default (GitHub's REST documentation for repositories). - The setting in GraphQL: a GraphQL repository has
deleteBranchOnMerge, whether or not branches are automatically deleted when merged in that repository (Repository). - Open pull requests: with write access you can delete branches associated with closed or merged pull requests, but not branches associated with open pull requests; the "Delete branch" button is not shown while there is an open pull request for the branch (GitHub's deleting and restoring branches in a pull request).
- Through the API: "Delete a reference" answers 204 when it deletes the branch, 409 for a conflict, and 422 for "Validation failed, an attempt was made to delete the default branch, or the endpoint has been spammed" (GitHub's REST API endpoints for Git references).
- A token for deleting it: "Delete a reference" works with GitHub App user access tokens, GitHub App installation access tokens and fine-grained personal access tokens, and the token must have the Contents repository permission (write) (GitHub's Delete a reference).
Reference does not exist(422): the reference named in the URL is not there, so there is nothing to delete or move. The response body has"message": "Reference does not exist"and adocumentation_urlending in#delete-a-referenceor#update-a-reference(public reports, release-plz/release-plz#2376, peter-evans/create-pull-request#4502). Check the name aftergit/refs/, such asheads/BRANCH, and whether the branch is already gone, for example deleted automatically after the merge.- The default branch: GitHub's documentation says to choose a new default branch before deleting the one that is the default (deleting a branch); the API's 422 above covers the attempt.
- gh pr merge --delete-branch:
-ddeletes the local branch and the branch on GitHub after the merge, but the GitHub CLI leaves the branch on GitHub alone when the pull request comes from another repository (a fork), or when the command only turns on auto-merge (--autoon a pull request that cannot merge yet). On a branch that requires a merge queue it refuses withCannot use `-d` or `--delete-branch` when merge queue enabled. When GitHub answers the deletion with 422 or 404, as for a branch that was already deleted automatically, it takes the branch as gone and still printsDeleted remote branch BRANCH. With--repoit does not touch the local branch, and in some worktree layouts it leaves it with a warning that ends inskipping local delete(merge.go in cli/cli v2.103.3).
To delete the branch with the merge: Pull Request Inbox for GitHub, a plugin for JetBrains IDEs, can delete a pull request's branch after the merge from its merge dialog, and if GitHub refuses, says so and names the rules that protect branches from deletion.
After the head branch is deleted
- Pull requests based on it: when a head branch is deleted after its pull request was merged, GitHub changes the base branch of the open pull requests in the same repository that were based on it to the merged pull request's base branch (GitHub's merging a pull request).
- Restoring it: the head branch of a closed pull request can be restored with "Restore branch" near the bottom of the pull request.
- Bring it back without the button: for a branch that was not the head of a pull request, create the reference again at the commit it had:
gh api -X POST repos/OWNER/REPO/git/refs -f ref=refs/heads/BRANCH -f sha=SHA.refmust be the full name, starting withrefsand with at least two slashes; GitHub answers 201 when it creates the branch, and 409 or 422 when it does not. A new reference cannot be created in an empty repository, one without branches (GitHub's create a reference). - Who deleted it, and when: in the REST API's issue events and timeline events of a pull request,
head_ref_deletedmeans its head branch was deleted, andhead_ref_restoredthat the branch was restored to the last known commit (GitHub's issue event types). In GraphQL,HeadRefDeletedEventhas the actor, the time andheadRefName(GitHub's HeadRefDeletedEvent). - A restore, in GraphQL: a
head_ref_restoredevent on a pull request is a GraphQLHeadRefRestoredEvent, withactor, who performed it, andcreatedAt(HeadRefRestoredEvent). - A deletion with no pull request: the REST API's list of repository activities,
gh api "repos/OWNER/REPO/activity?activity_type=branch_deletion&ref=BRANCH", shows each deletion with itsactor, itstimestamp, and the commits before and after it (before,after).reftakes a branch name orrefs/heads/BRANCH, andactorandtime_period(daytoyear) narrow it further (GitHub's list repository activities).
What to do
- Find the rule: anyone with read access to a repository can view its active rulesets; look for one whose pattern matches the branch and that restricts deletions. A branch protection rule for the branch keeps it unless "Allow deletions" is on.
- Delete it later: someone allowed to bypass the ruleset, or with write access when the protection allows deletions, clicks "Delete branch" on the merged pull request. For a fork's branch, it takes write access to the fork.
- The ruleset should not cover these branches: whoever manages the ruleset narrows its pattern, or removes "Restrict deletions" from it.
- Another pull request still uses the branch: merge or close that pull request first.
- From the Branches page instead: the trash icon next to the branch deletes it, and when the branch is associated with at least one open pull request, deleting it closes those pull requests; GitHub shows a warning first. Its documentation says to merge or close the pull request before deleting the branch (deleting a branch).
- From git:
git push origin :BRANCHdeletes the branch on the remote; GitHub's documentation points out the space before the colon, which tells Git to push nothing into that branch.git push origin --delete BRANCHdoes the same: git describes--deleteas the same as prefixing every ref with a colon (GitHub's deleting a remote branch or tag and git's git push). The rules above still decide, so a protected or locked branch cannot be deleted this way either (see GitHub push refused). - Hear about a deletion: the
deletewebhook occurs when a Git branch or tag is deleted. Its payload hasref,ref_type(branchortag) andpusher_type, and no event is created when more than three tags are deleted at once (GitHub's delete webhook). - In a workflow:
on: deleteruns when someone deletes a branch or tag in the repository, and only if the workflow file exists on the default branch.GITHUB_REFis then the default branch andGITHUB_SHAits last commit, so the deleted branch is the payload'sref, read asgithub.event.ref(GitHub's events that trigger workflows and github context).
In IntelliJ IDEA: "has been deleted." while the branch is still there
Checked in the IntelliJ IDEA source on October 31, 2026: GHPRFileEditorComponentFactory.kt, GHPRDetailsTimelineViewModel.kt, GHPRDeleteMergedHeadAction.kt, GHPRDetailsServiceImpl.kt, deleteRef.graphql, GithubBundle.properties.
After the merge, the pull request's timeline shows an item headed "Pull Request Successfully Merged" with one of these two lines:
You can now delete BRANCH.
BRANCH has been deleted. Restore…
The IDE's merge sends only the merge, and it deletes the branch only when you ask. The first line, with the action Delete Branch ("Delete the merged branch on GitHub"), appears only while the pull request is merged, GitHub returns viewerCanDeleteHeadRef as true for your account, and the head branch still has an id.
In every other case the item shows the second line, and Restore… only opens the pull request in your browser. So that line also appears for a branch that is still there. Look at the branch on GitHub, then use the rules above.
The deletion goes through GitHub's GraphQL API (deleteRef on the head branch), on GitHub only: your local branch stays as it is. When GitHub refuses, nothing appears on screen and the error goes to the IDE's log. The pull request is then loaded again, so the line follows the branch's state on GitHub.
GitHub pull requests in IntelliJ IDEA
Pull Request Inbox for GitHub, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, shows a pull request's reviews, checks and conversations next to the diff, and merges it with the methods the target branch allows. Its merge dialog can delete the pull request's branch after the merge (a branch in the same repository, not a fork's). If GitHub refuses, the notice after merging says the branch was not deleted and names the rules that protect branches from deletion; a refusal answered with 422 too, as the plugin then looks whether the branch is still there. The review inbox, diffs and conversations are free; approving, commenting, resolving and merging are Pro (US$1.50 a month, with a 30-day trial).
Get it on JetBrains Marketplace
GitHub is a trademark of GitHub, Inc.; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by GitHub, Inc. or JetBrains.
More: GitHub merge refused (405, 409) · GitHub review refused (422) · GitHub search 422 "Validation Failed" · "Resource not accessible by personal access token" · 401 "Bad credentials" · "Resource protected by organization SAML enforcement" · "API rate limit exceeded" · GitHub diff 406 "too_large", 3,000 files · GitHub line comment 422 "must be part of the diff" · GitHub branch update refused (422) · GitHub pull request not created (422, 403) · GitHub auto-merge refused · GitHub draft, ready for review or close refused · GitHub conversation not resolved · GitHub checks not shown (check runs 403) · GitHub file contents not read · GitHub compare refused (404, 422) · GitHub comment or reply refused (403 locked, archived) · GitHub merge method not allowed (405) · GitHub branch not found (404, 301) · GitHub Enterprise Server not reached · GitHub review request not shown · GitHub pull request list incomplete · GitHub 404 Not Found, renamed repositories (301, 307) · GitHub line comment outdated or not on the diff · GitHub pull requests not updating, notifications late · GitHub suggested change cannot be applied · GitHub required check stuck at "Expected" · GitHub code owner review required · GitHub merge blocked by unresolved conversations · GitHub merge blocked by unsigned commits · GitHub branch that merges through a merge queue · GitHub merge waiting for an approval of the most recent push · GitHub merge waiting for code scanning results · GitHub merge waiting for code quality results · GitHub merge blocked by a code coverage threshold · GitHub merge blocked by license compliance results · GitHub branch locked or its updates restricted · GitHub merge waiting for a team's approval (Required reviewers) · GitHub merge blocked by secret scanning alerts · GitHub stacked pull request not merging · GitHub push blocked by push rules · GitHub merge blocked by commit metadata rules · GitHub force push blocked · GitHub issue not closed by a merged pull request · GitHub pull request not checked out locally · GitHub pull request has merge conflicts · GitHub changes requested and dismissing a review · GitHub co-author not credited for a commit · GitHub dependency review check failed · GitHub Dependabot not rebasing its pull request · GitHub fork not syncing with upstream · GitHub pull request base branch not changing · GitHub pull request not reverting · GitHub pull request template not showing · GitHub pull request labels or assignees not added · GitHub pull request not closing or reopening · GitHub push blocked by push protection · GitHub pull request comment hidden · GitHub pull request blocked until deployments succeed · GitHub team review request replaced by members · GitHub pull request file no longer marked Viewed · GitHub pull request marked merged without being merged · GitHub Copilot review does not count as an approval · GitHub checks not running on a pull request a workflow created · GitHub Update branch button not shown on a pull request · GitHub checks failing on a Dependabot pull request · GitHub checks failing on a pull request from a fork · GitHub checks skipped by [skip ci] in a commit message · GitHub checks gone after the retention period · GitHub code review limits in a public repository · GitHub pull requests turned off or limited in a repository · GitHub checks run on every pull request in a stack · GitHub pull request showing commits already squash merged · GitHub pull request not showing a file diff · GitHub push to a pull request branch from a fork refused · GitHub check cancelled on a pull request · GitHub Rebase and merge not available · GitHub merge commit message not what you expected · GitHub pull request shows every line changed · GitHub API request timed out · GitHub pull request image or document diff not shown · GitHub review request refused (422) · GitHub pull request from Copilot: checks not running, approval not counted · GitHub push refused: GH006 and GH013 on a protected branch · GitHub "At least 1 approving review is required", approvals that do not count · GitHub pull request plugins for IntelliJ IDEA, compared · 日本語