How to remove merged feature branches in Azure DevOps?
Asked Answered
L

2

8

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)

Cannot delete a branch which has policies

Lola answered 12/8, 2020 at 8:25 Comment(3)
Are you adding the policy one by one from the UI?Asymmetry
No. Policy is for feature/* branches.Lola
@LassiAutio Have you been able to resolve this issue?Hydrophane
J
7

There is already a known user voice for this scenario, you can vote it in the following link:

https://developercommunity.visualstudio.com/idea/905484/allow-to-delete-branch-after-pull-request-in-a-bra.html

Currently, you would either delete the feature branch from UI separately, or disable the branch policy before completing pull request.

Jackal answered 13/8, 2020 at 6:40 Comment(2)
Have you checked my reply? Is it helpful for you?Jackal
Same problem here. Would be useful to know how Microsoft deals with this? Is Microsoft not using branch policy?Hydrophane
S
3

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:

enter image description here

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:

enter image description here

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.

Shackle answered 26/7, 2023 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.