Search code examples
azure-devopsazure-repos

Cannot delete a branch which has policies


Consider the following branch policy set at the project level:

Project branch policies

How to delete a releases/xxxxx branch that was created by accident?

When trying to do it using the portal an error message is displayed (as expected):

Cannot delete a branch which has policies

I'd like to avoid disabling the branch policy, if possible.

The error message mentions some sort of exemption, but I couldn't find any documentation about it (it would be great to have a link to the documentation when the message is displayed).


Solution

  • You can adjust the security for the specific branch you would like to delete (rather than at the "folder" level). In the 3-dots menu, select "Branch Security", and then assign yourself "Allow" for both of these permissions:

    • Bypass policies when pushing
    • Force push

    Then you will be able to delete the branch.

    Side Note: This answer works when you only need to delete a small number of branches (like in your case). But if you need to delete many branches that have policies set at the folder level, you'll probably want to temporarily disable the policy, delete the branches, and then put the policy back. This is because (unfortunately) it's not possible to set security at the folder level, like you can with policies.