How to Delete a Branch in Azure DevOps

Deleting a branch in Azure DevOps isn’t just about simply deleting; it’s about maintaining and preserving workflow. Whether you are a GUI loyalist or a person who likes the Command Line, I’m going to walk you through exactly how to delete branches in Azure DevOps, ensuring you do it safely and effectively.

How to Delete a Branch in Azure DevOps

Why You Can’t Just Hit “Delete” (Sometimes)

Before we jump into the “how,” we need to address the “why not.” If you have tried to delete a branch and failed, it is usually due to one of two gatekeepers: Permissions or Policies.

  • Permissions: To delete a remote branch, you generally need “Force push” or specific “Delete” permissions on that repository. If you are a standard “Contributor,” you can usually delete branches you created, but you might be blocked from deleting others.
  • Branch Policies: You cannot delete the Default branch (usually main or master). Azure DevOps simply won’t let you until you change the default setting to another branch.

Check out: How To Recover Deleted Branch In Azure DevOps

Method 1: The Web Portal (The Easiest Way)

This is the method I use 90% of the time. It is visual, safe, and gives you a second to think before you commit to the deletion.

  1. Navigate to Repos: Log in to your Azure DevOps organization and open your project.
  2. Go to Branches: On the left-hand sidebar, hover over Repos and click Branches.
  3. Locate Your Branch: You will see tabs for “Mine” (branches you created) and “All”. If your branch is old, you might need to use the search bar at the top right.
  4. The “More actions” menu: Hover over the row of the branch you want to remove. You will see a more actions (three dots) menu appear on the far right.
  5. Confirm: Click the delete branch option. Azure DevOps might ask for confirmation. Once you click delete, the branch is removed from the remote repository immediately.

Check out the screenshots below for the complete steps:

How to Delete a Branch in Azure DevOps

Pro Tip: If you don’t see the trash can, click the three dots (…) next to the branch name. “Delete branch” will be an option in the dropdown menu.

Method 2: Visual Studio 2022

If you are working in a .NET shop, you likely live inside Visual Studio. You don’t need to leave your IDE to clean up your remote branches.

  1. Open the Git Menu: In the top menu bar, click Git.
  2. Manage Branches: Select Manage Branches from the dropdown. This opens a dedicated window that is far superior to the old Team Explorer view.
  3. Find the Remote Branch: In the sidebar, look under the remotes/origin folder.
    • Note: Deleting a branch under local only removes it from your machine. To actually delete it from Azure DevOps, you must target the remote branch.
  4. Right-Click and Delete: Right-click the branch name and select Delete.
  5. Confirm the Push: Visual Studio allows you to delete the remote branch directly. It basically sends a command to the server to strip that reference.

Check out the below screenshot for reference:

how to delete branch in azure devops

Method 3: The Command Line (The Power User Way)

For those of us who prefer the terminal, deleting a branch is a two-step process. You often have a local copy on your laptop and the remote copy on the Azure DevOps server. You usually want to kill both.

Step 1: Delete the Local Branch

First, ensure you are not currently checked out to the branch you want to delete. Switch to main first.

Bash

git checkout main
git branch -d feature/old-login-page
  • git branch -d is the safe delete. It will warn you if you have unmerged changes.
  • git branch -D (capital D) is the force delete. It destroys the branch even if work hasn’t been saved. Use with caution.

Step 2: Delete the Remote Branch (Azure DevOps)

This is the command that actually removes the branch from the server so your colleagues stop seeing it.

Bash

git push origin --delete feature/old-login-page

Once this command runs successfully, you will see a confirmation message like [deleted] feature/old-login-page.

Method 4: Automating Deletion via Pull Requests

The best way to delete branches is to never have to “remember” to do it. You should configure your Pull Request (PR) workflow to handle this for you.

When you are completing a PR in Azure DevOps:

  1. Look at the completion dialog box.
  2. Check the box that says “Delete source branch after merging”.
  3. Hit Complete Merge.

This automatically tidies up the repository the moment the code is safely merged into main. I highly recommend making this a team policy. It prevents the “I’ll delete it later” procrastination that leads to 400 stale branches.

How to Restore a Deleted Branch (The “Undo” Button)

We have all been there. You deleted feature/critical-update thinking it was feature/critical-update-backup, and your heart drops.

The good news is that Azure DevOps is forgiving. Soft deletes are the default.

  1. Go to the Branches Page: Back in the Azure DevOps web portal.
  2. Search for the EXACT Name: In the branch search box, type the exact name of the branch you just deleted.
  3. Find in Deleted Branches: A link will appear below the search box saying “Search for exact match in deleted branches”. Click it.
  4. Restore: Your deleted branch will appear in the results. Click the three dots (…) next to it and select Restore branch.

It will be resurrected with all its history and commits intact.

Comparison of Deletion Methods

MethodSpeedSafetyBest For
Web PortalFastHighSingle branch cleanup; Visual confirmation.
Visual StudioMediumHigh.NET developers who don’t want to switch windows.
Command LineInstantLow (if careless)Bulk deletion; Scripting; Power users.
PR Auto-DeleteAutomatedHighestDay-to-day workflow hygiene.

Troubleshooting: “Why Can’t I Delete This Branch?”

If you are hitting errors, check these common blockers:

  • Error: “The branch is locked.”
    • Fix: Someone has explicitly locked the branch to prevent changes. Look for a “Lock” icon next to the branch name in the web portal. Right-click and select Unlock (if you have permission).
  • Error: “TF401027: You need the Git ‘ForcePush’ permission…”
    • Fix: This usually happens when trying to delete via CLI. Check your repository security settings. You need the Force push (rewrite history, delete branches and tags) permission set to Allow.
  • Error: “Cannot delete the default branch.”
    • Fix: You are trying to delete main or master. You must go to Project Settings > Repositories, select your repo, and change the default branch to something else before you can delete the old one.

Conclusion

Keeping your Azure DevOps repository clean is a habit, not a one-time task. By using the “Delete source branch” feature in Pull Requests and periodically auditing your branches using the Web Portal, you can keep your engineering velocity high and your confusion low.

Whether you click the trash can or type git push origin --delete, the result is the same: a cleaner, more professional codebase.

You may also like the following articles:

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!