6

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

Lassi Autio
  • 1,147
  • 1
  • 13
  • 35

2 Answers2

6

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.

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
2

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

In 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:

Cannot delete a branch which has policies

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:

Branch policy and Delete branch option