Bitbucket did not delete the source branch after merging
Both Bitbucket Cloud and Bitbucket Data Center can delete a pull request's source branch when it is merged, if they are asked to, and each decides at a different moment. When the branch is still there, it is usually one of five things: the option was off when the merge happened, a script merged without asking for it, a branch permission kept it, it is the default branch, or the branch is gone from Bitbucket and only your local Git still lists it.
Bitbucket Cloud: Close source branch
- The choice is stored on the pull request (
close_source_branch). Cloud's API definition says that when a merge does not send it, Bitbucket uses "the value used when the pull request was created, which defaults to False". - It cannot be changed on the pull request after it is created: the request to allow that (BCLOUD-23488, November 2024) is still gathering interest. Its workaround is to tick Close source branch in the merge dialog.
- Scripts: merging with
POST …/pullrequests/{id}/mergeand no body used to overwrite the stored value with false, so the branch stayed (BCLOUD-17883, marked fixed in January 2023). Sending the field yourself is the safe way: a JSON body with"close_source_branch": trueand theContent-Type: application/jsonheader.
To close the source branch with every merge: Pull Request Inbox for Bitbucket, a plugin for JetBrains IDEs, has Close {branch} after merging in its merge dialog, and on Cloud sends close_source_branch with every merge, so the value stored on the pull request does not decide it.
Bitbucket Data Center: Delete source branch after merging
- In the web interface it is a box in the merge dialog. Atlassian's documentation (Merge a pull request, 8.13) says open pull requests that target the source branch are retargeted to the target branch, and the branch is not deleted when it is the repository's default branch or when you do not have permission to delete it.
- The REST API does not delete it.
POST …/pull-requests/{id}/mergeonly merges; the branch goes with a second call,DELETE /rest/branch-utils/latest/projects/{project}/repos/{repo}/brancheswith{"name": "refs/heads/feature/login", "dryRun": false}. Atlassian's API definition (9.4) says it needs write permission and, where branch permissions are set, access to that branch name; it answers 401 when either is missing, and 400 for the default branch.
What to check
- Is it gone from Bitbucket? Look at the repository's branch list. If it is gone there, your Git only keeps a remote-tracking copy:
git fetch --pruneremoves copies of branches that no longer exist on the server. - Was the option on at merge time? On Cloud, the pull request in the API has
close_source_branch; on Data Center the box is in the merge dialog. - A branch permission (Data Center) or branch restriction (Cloud) that does not let you delete the branch can keep it. Delete it as someone who is allowed, or leave it if the rule is meant to keep it.
- A script: on Cloud, send
close_source_branchwith the merge; on Data Center, call branch-utils after the merge succeeded. - To delete it by hand: the branch list on the web, or
git push origin --delete feature/login. If Bitbucket Cloud will not let you, see below.
Bitbucket Cloud will not delete it by hand
In the repository's Branches list, the Delete action is greyed out, or deleting answers We were unable to delete the <branch> branch. From Atlassian's knowledge base (The 'delete' button is disabled and unable to delete the branch from the repository, Error "Unable to delete branch" in Bitbucket Cloud):
- Hover over the greyed Delete. It says why. The repository's main branch cannot be deleted at all.
- A branch restriction. In Repository settings > Branch restrictions, look for a restriction whose pattern matches the branch and says deleting is not allowed. A repository admin can edit it and tick Allow deleting this branch.
- The write access list. If that restriction has Only specific people or groups have write access, only the people on the list can delete the branch; add the person who has to.
- Open pull requests into the branch. Filter the Pull requests list by the branch as the destination, and merge or decline them first.
- Delete is there but fails. Atlassian says the branch may have lock files on its side; contact Bitbucket Cloud support.
Bitbucket pull requests in IntelliJ IDEA
Pull Request Inbox for Bitbucket, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the pull requests that wait for your review in an IDE tool window, from Bitbucket Cloud and, in beta, from Bitbucket Data Center 8.9 or later with a personal HTTP access token, sent only to that server. Its merge dialog has Close {branch} after merging, starting from a setting you choose once. On Cloud it sends close_source_branch with every merge, so the value stored on the pull request does not decide it. On Data Center it merges first and then deletes the branch with branch-utils; when Bitbucket refuses the deletion, it says the pull request was merged and the branch was not deleted, with Bitbucket's reason. The review inbox, diffs and discussions are free; merging, approving and commenting are Pro (US$1.50 a month, with a 30-day trial).
Get it on JetBrains Marketplace
Bitbucket and Atlassian are trademarks of Atlassian; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Atlassian or JetBrains.
More: Restore a deleted branch · Bitbucket merge strategies · pre-receive hook declined · Bitbucket pull request plugins for IntelliJ IDEA, compared · 日本語