How to Delete Branches in GitHub, GitLab, Azure DevOps & Bitbucket | Automatically After Merge

|

Learn how to delete Git branches in GitHub, GitLab, Azure DevOps, and Bitbucket with easy step-by-step instructions and screenshots. This guide also covers auto-delete after merge, branch cleanup, permissions, and common branch deletion issues.

Git
 UI + CLI
12 min read

Keeping a repository clean is one of those things every team knows they should do — and few actually do consistently. Stale branches accumulate over time, making it harder to navigate a project and understand what’s actively in development. Whether you’re a solo developer or part of a large engineering team, knowing how to properly delete branches in GitHub, GitLab, Azure DevOps, and Bitbucket is a fundamental maintenance skill.

This guide covers everything: deleting branches from the UI, from the command line, setting up automatic cleanup after merges, dealing with protected branches, and recovering branches you deleted by mistake.

How to Delete a Branch in GitHub

GitHub

Branches can be deleted via Pull Requests, the Branches page, or the GitHub CLI — with built-in restore support.

GitHub provides several ways to perform a GitHub delete branch operation. The most common are through a merged pull request, through the Branches management page, or via the GitHub CLI. Each method is appropriate for different workflows.

Delete a Branch from a Pull Request

The fastest and most common workflow — once a pull request is merged, GitHub offers a one-click option to remove the source branch immediately.

  1. Navigate to the Pull Requests tab in your repository.
  2. Open the merged pull request you want to clean up.
  3. Scroll to the bottom and click “Delete branch” — it appears automatically after the merge is complete.

Success Output

Branch deleted
Restore this branch if it was deleted by mistake (available for 30 days)

To delete a remote branch via the command line, use the git push command with the --delete flag:

Delete remote branch on GitHub
# Delete a remote branch on GitHub
git push origin --delete feature/user-auth

# Or the equivalent shorthand
git push origin :feature/user-auth

# Also delete the local tracking branch
git branch -d feature/user-auth

Delete a Branch from the Branches Page

If you need to delete branches in bulk or outside of a pull request context, the Branches page gives you a full view of all branches.

  1. Go to your repository and click the branch dropdown (shows your current branch name) near the top left.
  2. Click “View all branches” at the bottom of the dropdown.
  3. On the Branches page, find the branch to delete and click the 🗑 trash icon on the right.
  4. Confirm the deletion when prompted.

Filtering Stale Branches

The Branches page lets you filter by “Stale” — branches with no commits for 3+ months. This is a great starting point for bulk cleanup without reviewing every branch manually.

Why GitHub May Prevent Branch Deletion

There are a few reasons a GitHub delete branch operation might be blocked:

  • Default branch protectionmain or master cannot be deleted without first changing the default branch in repository settings.
  • Branch protection rules — any branch matching a protection rule pattern (e.g., release/*) is protected from deletion unless the rule is modified.
  • Insufficient permissions — only users with Write access or higher can delete branches; Read-only collaborators cannot.
  • Open pull request — GitHub will warn you if an open PR targets the branch you’re trying to delete.

Cannot Delete Default Branch

To delete a branch that is currently set as the repository default, first go to Settings → General → Default branch and switch the default to another branch. Only then can you delete the old one.

Restore a Deleted GitHub Branch

Deleted a branch by accident? GitHub keeps deleted branches recoverable for up to 30 days.

  1. Go to the merged Pull Request that was associated with the branch.
  2. At the bottom of the PR page, click “Restore branch”.

If there was no pull request, you can restore via the git reflog from a local clone:

Restore deleted branch from reflog
# Find the last commit of the deleted branch
git reflog

# Recreate the branch at that commit SHA
git checkout -b feature/user-auth a1b2c3d4

# Push it back to GitHub
git push origin feature/user-auth

How to Automatically Delete Branches After Merge in GitHub

The GitHub auto delete branch after merge feature is one of the most underused repository settings. Enabling it removes the need for anyone to manually clean up branches after pull requests are merged — the platform handles it automatically.

Enable Auto Delete Branch in Repository Settings

  1. Go to your repository on GitHub.
  2. Click Settings in the top navigation bar.
  3. Scroll down to the Pull Requests section.
  4. Check the box next to “Automatically delete head branches”.

Setting Enabled

Automatically delete head branches: Enabled
Head branches will be automatically deleted after pull requests are merged.

Once enabled, GitHub will automatically delete the source (head) branch any time a pull request is merged into the repository. The option to restore still appears on the merged PR page for 30 days.

Why Auto Delete Helps Keep Repositories Clean

📌 Real-world Scenario: Stale Branch Accumulation Common Problem

A team of 8 developers ships features every week. After 6 months, they have over 120 stale branches in their repository because no one remembered to delete them after merging. Navigating the branch dropdown becomes slow and confusing. New engineers can’t tell which branches are active.
Enabling auto delete after merge at the repository level takes 10 seconds and prevents this entirely. Every merged PR automatically removes its source branch, keeping the branch list short and meaningful.

GitHub Permissions Required for Auto Delete

To toggle the auto delete branch setting, a user needs Admin access to the repository. Regular contributors with Write access cannot change repository settings.

RoleDelete Own BranchDelete Any BranchEnable Auto Delete
Read
Triage
Write (non-protected)
Maintain
Admin

How to Delete a Branch in GitLab

GitLab

GitLab offers branch deletion via Merge Requests, the Repository branches UI, or the GitLab CLI — with configurable protected branch rules.

The GitLab delete branch process closely parallels GitHub’s, but there are some platform-specific settings worth knowing — especially around protected branch rules and auto-deletion options.

Delete a GitLab Branch from Merge Requests

GitLab allows you to delete a source branch as part of the merge flow, either at merge time or afterward.

  1. Open the Merge Request in your GitLab project.
  2. Before merging, check “Delete source branch when merge request is accepted” — this is configurable per MR.
  3. Click Merge. GitLab will merge and immediately delete the source branch.

GitLab Default Setting

GitLab project owners can set Delete source branch as the default behavior for all merge requests in Settings → General → Merge Requests. This way, the checkbox comes pre-ticked for every new MR in the project.
Delete remote branch on GitLab via CLI
# Delete a remote branch on GitLab
git push origin --delete feature/payment-api

# Using the GitLab CLI (glab)
glab repo branch delete feature/payment-api

# Clean up stale remote-tracking refs locally
git remote prune origin

Remove Branches from Repository Settings

  1. In your GitLab project, navigate to Repository → Branches in the left sidebar.
  2. Use the search field to filter branches. GitLab also has a “Stale branches” filter.
  3. Click the trash icon next to any branch you want to delete.
  4. GitLab will ask for confirmation before deleting.

Protected Branch Restrictions in GitLab

GitLab’s protected branch system is more granular than GitHub’s. A branch can be protected against both pushing and deletion independently. To delete a protected branch:

  1. Go to Settings → Repository → Protected branches.
  2. Find the protected branch rule for the branch in question.
  3. Click Unprotect or modify the rule to allow deletion for Maintainers.
  4. Return to the Branches page and delete normally.

GitLab Roles Required

In GitLab, only users with the Maintainer or Owner role can delete branches (for non-protected branches, Developers may also delete branches they created). Protected branches require Maintainer-level access at minimum to unprotect.

How to Delete a Branch in Azure DevOps

Azure DevOps

Azure Repos supports branch deletion via the UI, pull requests, and the Azure CLI — with branch policies that may restrict deletion.

For teams using Azure DevOps, performing an Azure DevOps delete branch is handled through Azure Repos. The process supports both UI-based deletion and command-line approaches via the Azure CLI or git directly.

Delete a Branch from Azure Repos

  1. In your Azure DevOps project, go to Repos → Branches.
  2. Search for or scroll to the branch you want to remove.
  3. Click the ⋯ (More options) button on the right side of the branch row.
  4. Select “Delete branch” from the dropdown menu.
Azure DevOps — delete branch via CLI
# Standard git approach (works with any remote, including Azure)
git push origin --delete feature/reporting-dashboard

# Using the Azure CLI (az devops extension)
az repos ref delete \
  --name refs/heads/feature/reporting-dashboard \
  --repository MyRepo \
  --project MyProject \
  --organization https://dev.azure.com/MyOrg

# Prune stale references locally
git fetch --prune origin

Remove Completed Feature Branches After Pull Requests

Azure DevOps can be configured to automatically delete source branches when pull requests are completed. This is done per-pull request or as a repository-level default.

🔧 Setting Auto Delete on a Pull Request Azure PR Settings

When completing a pull request in Azure DevOps, a modal dialog appears with completion options. Check “Delete source branch [branch name] after this pull request is completed”. Azure will automatically remove the branch once the PR is successfully merged.
To make this the default for all PRs in a repository, navigate to Project Settings → Repositories → Policies and enable “Delete source branch” as a default completion option.

Branch Policies That Block Deletion

Azure DevOps branch policies are among the most comprehensive of any Git platform. Policies that can block an Azure DevOps delete branch operation include:

  • Minimum reviewers — branches with active PRs requiring a minimum number of reviewers cannot have their target branch deleted.
  • Linked work items — policies that enforce work item linking may prevent deletion if linked items are still open.
  • Build validation — if a branch has an active build policy, it is generally considered production-critical and protected.
  • Lock branch — a branch can be explicitly locked by a repo admin, preventing any pushes or deletions.

Locked Branches in Azure

A locked branch in Azure DevOps cannot be deleted until unlocked. Go to Repos → Branches → ⋯ → Lock/Unlock to toggle the lock status. Only users with the Contribute permission or higher can lock/unlock branches.

How to Delete a Branch in Bitbucket

Bitbucket

Bitbucket Cloud supports branch deletion from the Source view, pull requests, and the Bitbucket CLI — with branch permissions for fine-grained control.

The Bitbucket delete branch process works through the Source view, pull request completion, or the Bitbucket CLI. Bitbucket Cloud and Bitbucket Data Center differ slightly in their interfaces, though the core concept is the same.

Delete Branches in Bitbucket Cloud

  1. Navigate to your Bitbucket repository.
  2. Click Source in the left sidebar, then select Branches from the branch dropdown.
  3. Click the  icon next to the branch you want to delete.
  4. Select “Delete branch” and confirm.
Bitbucket — delete branch via git and bb CLI
# Delete remote branch on Bitbucket (standard git)
git push origin --delete feature/checkout-flow

# Using the Bitbucket CLI
bb branch delete feature/checkout-flow

# Also remove local tracking reference
git branch -dr origin/feature/checkout-flow

Delete Merged Branches Safely

Bitbucket Cloud allows branch deletion directly from a merged pull request, similar to GitHub. When a PR is merged, Bitbucket displays a “Delete branch” button in the PR overview. This is the safest way to delete a branch because:

  • The merge is confirmed before deletion — you won’t accidentally delete an unmerged branch.
  • Bitbucket keeps a record in the PR audit log that the branch was deleted post-merge.
  • You can restore the branch from the same PR page if needed.

Bitbucket Branch Permissions Explained

Bitbucket Cloud uses branch permissions to control who can read, write to, and delete specific branches. These are configured at the repository level.

  1. Go to Repository Settings → Branch permissions.
  2. Click Add a branch permission.
  3. Set a branch pattern (e.g., mainrelease/*) and configure which users or groups can delete it.

Bitbucket Branch Permission Model

Bitbucket lets you separately control Write access and Delete access. A developer can have push access to a branch without having the ability to delete it. This is ideal for protecting long-lived branches like develop or staging.

Common Branch Deletion Problems and Fixes

Whether you’re working with GitHub, GitLab, Azure, or Bitbucket, the same categories of issues tend to block branch deletion. Here’s how to diagnose and resolve each one.

Cannot Delete Protected Branch

🛡️ Problem: Branch is Protected Blocked

The branch matches a protection rule that prevents deletion. This is common for main, master, develop, or any branch matching a wildcard pattern like release/*.
Fix: Go to repository settings → Branch protection rules (GitHub) / Protected branches (GitLab) / Branch policies (Azure) / Branch permissions (Bitbucket). Either remove the rule or exempt the specific user who needs to delete the branch.

Error

! [remote rejected] feature/old-flow -> feature/old-flow (protected branch hook declined)
error: failed to push some refs to ‘git@github.com:org/repo.git’

Missing Permissions to Delete Branch

🔐 Problem: Insufficient Access Rights Permissions

The authenticated user does not have Write or Maintainer-level access to the repository, or the specific branch permission explicitly restricts deletion for their role.
Fix: Request the repository admin to elevate your access, or ask them to perform the deletion on your behalf. In GitHub and Bitbucket, repository owners can also grant Write access to specific collaborators without making them full administrators.

Branch Still Appears After Deletion

If you’ve deleted a remote branch but it still shows up in your local git branch list, your local repository hasn’t pruned its stale remote-tracking references.

Fix: prune stale remote-tracking branches
# Fetch and prune deleted remote branches
git fetch --prune origin

# Or configure git to prune automatically on fetch
git config --global fetch.prune true

# List all local tracking branches that no longer exist remotely
git branch -vv | grep ': gone]'

# Delete all gone tracking branches in one step
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d

Remote Branch Was Already Removed

When you try to delete a remote branch that was already deleted by a teammate, git returns a cryptic error:

Error

error: unable to delete ‘feature/old-api’: remote ref does not exist
error: failed to push some refs to ‘git@github.com:org/repo.git’

This is harmless — the branch is already gone from the remote. Run git fetch --prune origin to sync your local reference list and the error will resolve itself.


Best Practices for Branch Cleanup

Delete Feature Branches After Merge

Make branch deletion a mandatory part of your PR completion checklist. No merged branch should stay open longer than 24 hours.

Protect Long-Lived Branches

Add protection rules to maindevelop, and any release branches. These should never be accidentally deleted.

Enable Auto Delete at Repo Level

Don’t rely on individuals to remember cleanup. Enable auto delete after merge at the repository settings level on every platform that supports it.

Schedule Quarterly Branch Audits

Set a recurring calendar reminder to review stale branches (older than 90 days with no activity) and clean them out as a team.

Use Branch Naming Conventions

A consistent naming pattern like feature/fix/hotfix/ makes it much easier to identify which branches are candidates for cleanup.

Configure git fetch.prune Globally

Run git config --global fetch.prune true on all developer machines so local tracking branches stay synchronized with the remote automatically.

Delete Feature Branches After Merge

The single most impactful habit for repository hygiene is establishing a team norm that every feature branch gets deleted after its PR is merged. Whether you do this manually by clicking “Delete branch” or automatically via repo settings, the result is the same: a branch list that only contains branches representing active, ongoing work.

Keep Main and Production Branches Protected

Your main, master, production, and release/* branches should always have deletion protection enabled. On every platform covered in this guide, this takes fewer than five minutes to configure and can prevent a catastrophic accidental deletion.

Use Auto Delete for Cleaner Repositories

The GitHub auto delete branch after merge feature — and equivalent settings in GitLab, Azure DevOps, and Bitbucket — effectively enforces the “delete after merge” practice automatically, removing the human memory requirement from the equation. For most teams, enabling this is the single highest-leverage improvement to their branch hygiene workflow.

Recommended Baseline Settings

For any active repository: enable auto delete after merge, protect main and any release branches from direct deletion, configure fetch.prune true for all contributors, and establish a 90-day stale branch review cadence.

Frequently Asked Questions

Does deleting a branch delete commits?

No. Deleting a branch only removes the branch pointer — the label that points to a commit history. The underlying commits remain in the repository’s object store and are accessible via their SHA hashes. Git’s garbage collection process will eventually clean up commits that are truly unreachable by any branch, tag, or ref, but this does not happen immediately. Any commits that were merged into another branch (like main) are permanently safe.

Can I recover a deleted branch?

Yes, in most cases. On GitHub, deleted branches are recoverable for 30 days from the merged PR page. On GitLab, the same is true from the Merge Request. In Azure DevOps and Bitbucket, recovery depends on whether you have a local clone with the commits in its reflog. Locally, run git reflog to find the last commit SHA of the deleted branch and recreate it with git checkout -b branch-name SHA.

Who can delete branches in GitHub or Azure DevOps?

In GitHub: Users with Write access or higher can delete non-protected branches. Admin access is required to delete protected branches or change branch protection rules.

In Azure DevOps: Users with the Contribute permission on a repository can delete branches. Protected branches (those with branch policies applied) require a user with the Exempt from policy enforcement permission or an administrator to bypass the policy.

Should merged branches be deleted?

Yes, as a general rule. Once a branch has been merged into your default branch, it has served its purpose. Keeping it around adds noise to the branch list without providing value — the commit history is permanently preserved in the target branch. The only exception might be long-lived maintenance branches (like v1-lts) that you plan to keep active for hotfixes over an extended period.

Conclusion

Deleting branches is a small action with a meaningful cumulative impact on repository health. Whether you’re working in GitHub, GitLab, Azure DevOps, or Bitbucket, the core workflow is straightforward — but knowing where to find the settings, how to handle permissions, and what to do when things go wrong is what separates a well-maintained codebase from a tangled one.

Quick Recap of Branch Deletion Methods

ToolHow to deleteHas Auto delete
🐙 GitHubVia PR “Delete branch” button, Branches page trash icon, or git push origin --delete
Auto delete
🦊 GitLabVia MR completion checkbox, Repository → Branches trash icon, or glab repo branch deleteAuto delete
☁️ Azure DevOpsVia Repos → Branches ⋯ menu, PR completion modal, or az repos ref deleteAuto delete
🪣 BitbucketVia Source → Branches ⋯ menu, PR “Delete branch” button, or bb branch deleteAuto delete

Recommended Cleanup Workflow for Teams

  1. Enable auto delete after merge in repository settings on every platform (Admin access required).
  2. Add branch protection rules to main, develop, and all release branches to prevent accidental deletion.
  3. Configure git config --global fetch.prune true on every developer machine to keep local refs synchronized.
  4. Run a quarterly stale branch audit using each platform’s “Stale branches” filter to catch any branches that auto delete may have missed.
  5. Document your branch naming convention and deletion policy in your team’s CONTRIBUTING.md so new engineers follow the same standards from day one.

Also read How to create branch in Git from current or another branch, Commit, Tag & Remote.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x