Azure DevOps did not delete the source branch after completing
Azure DevOps deletes a pull request's source branch on completion only when it is asked to. Deleting it then follows the same rules as deleting any branch on Azure DevOps. When the branch is still there, it is usually one of these: the option was off, the Force push permission on that branch is missing, the branch has required policies, it is the default branch or is locked, or it is gone from Azure DevOps and only your local Git still lists it. Below is from Microsoft's Complete, abandon, or revert pull requests, Delete a Git branch and Branch permissions.
Asking for it at completion
- On the web page: the Complete dialog has Delete <branch name> after merging among the post-completion options, next to Complete associated work items after merging. With auto-complete, the options are chosen when auto-complete is set and apply when Azure DevOps completes the pull request later.
- Azure CLI:
az repos pr update --id 21 --status completed --delete-source-branch true. The same flag goes with--auto-complete true(az repos pr). - REST API:
completionOptions.deleteSourceBranch, "If true, the source branch of the pull request will be deleted after completion" (Pull Requests - Update). A script that completes without it leaves the branch.
PATCH https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/21?api-version=7.1
{
"status": "completed",
"lastMergeSourceCommit": { "commitId": "<head of the source branch you reviewed>" },
"completionOptions": { "deleteSourceBranch": true, "mergeStrategy": "squash" }
}
If the source branch got new commits after that head was read, Azure DevOps refuses the completion with TF401192 and nothing is merged or deleted.
What keeps a branch from being deleted
Microsoft's Delete a Git branch lists what a remote branch needs before it can be deleted, "whether you delete the remote branch in the web portal, Visual Studio, or from the command line":
- Force push: the branch-level permission Force push (rewrite history, delete branches and tags) set to Allow. The permission's own description says it "is also required to delete a branch". Branch creators get it on their branch by default; Microsoft explains that most branches belong to one person, so no administrator is needed to delete a branch after a pull request merges. Someone who completes a colleague's pull request may not have it on that branch, and no security group has it by default at the repository level.
- Required branch policies: all of them have to be disabled or removed first, which needs the Edit policies permission. A source branch that is itself protected, such as a release branch merged into main, stays.
- The default branch cannot be deleted until another branch is made the default.
- A locked branch has to be unlocked first; unlocking another user's lock needs Remove others' locks.
What to check
- Is it gone from Azure DevOps? Look at Repos > Branches. 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? The completed pull request's
completionOptionsin the REST API shows what it was completed with. - The four conditions above: Microsoft's advice when Delete branch is not offered is to check your effective Force push permission and that the branch has no required policies, is not the default branch and is not locked.
- To delete it by hand: the branch's More options > Delete branch on Repos > Branches, or
git push origin --delete feature/login, by someone who has Force push on it.
In IntelliJ IDEA and Rider
Azure DevOps Pull Requests, a plugin by Lesstep for JetBrains IDEs 2024.1 or later and Android Studio, lists the pull requests of Azure DevOps Services and Azure DevOps Server in an IDE tool window. Its Complete dialog has Delete {branch} after merging, starting from a setting you choose once (Delete source branch after completing), and it sends deleteSourceBranch with every completion and with auto-complete, so the choice is never left to a default. It completes with the source commit it showed you, so new commits are not merged unseen. The four conditions above still decide whether Azure DevOps can delete the branch. Completing is a paid feature with a 30-day free trial; the review inbox, diffs and branch policies stay free.
Get it on JetBrains Marketplace
Azure DevOps is a trademark of Microsoft Corporation; IntelliJ IDEA and Rider are trademarks of JetBrains s.r.o. This page is not affiliated with or endorsed by Microsoft or JetBrains.
More: merge types · auto-complete · TF402455 · the same on Bitbucket · 日本語