We used to delete feature branches in pull requests automatically. But then we needed to add branch policy to feature branches and Azure DevOps says "Cannot delete a branch which has policies". Is there a way to delete all merged feature branches afterwards in the remote repo (ie. Azure Repo)? (Other than removing them one by one from UI)
There is already a known user voice for this scenario, you can vote it in the following link:
Currently, you would either delete the feature branch from UI separately, or disable the branch policy before completing pull request.
This is not a direct solution to your problem, but if you change the way you are using policies you may be able to workaround it. The biggest thing to understand is
Branch Policies apply to PRs based on the target branch, not the source branch.
According to the Azure DevOps Documentation:
A build validation policy queues a new build when a new PR is created or changes are pushed to an existing PR that targets the branch.
It's not explicitly called out for the rest of the policy types, but through some experimentation I have found that the other policies function the same way.
The wrong way to do things
If you're like me, you initially tried to configure DevOps to have a policy for all branches (In my case, this was a default set of reviewers). This creates a policy for every branch you create, and therefore prevents any branch from being deleted after a merge, like the following:
The "correct" way
In my case, we were merging temporary feature branches into a common, long-lived development branch. If you're doing something similar, don't put any policies on the temporary branches, put them on the long-lived branch instead. When we switched to having policies on the development branch, we were able to both have default reviewers and comment resolution policies, while also deleting branches during the merge process:
Edit: The documentation has been updated to reflect this more clearly
Branch policies are applied to Pull Requests based on the target branch of the Pull Request. Branch policies should not be set on temporary branches that will be deleted after a pull request. Adding branch policies to temporary branches will cause automatic branch deletion to fail.
© 2022 - 2024 — McMap. All rights reserved.