Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Is there a way to delete branches in bulk?

This question is in reference to Atlassian Documentation: Using branches in Bitbucket Server

As some developers tend to forget to delete their branches after they have been merged, we end up with a lot of stale branches that need to get deleted.  Is there a way for me to delete all of the branches that were last modified before a given date?  To prevent this problem from happening again, if you have any suggestions on how to manage so that users delete their branches after merging (the checkbox asking if they want to delete the branch seems to get overlooked).

4 answers

1 accepted

I presume you want to delete all branches that are already merged into 'master'?

You can do it from the command-line using a combination of 'git branch --merged origin/master' alongside the unix 'xargs' utility:

# Make sure remotes are up to date (with stale remotes purged):
git fetch -p
# Initial no-op run --- do the branches to delete look correct?
# Be careful to omit 'master' from the output.
git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs

# Do the bulk delete!!!  (can take a long time...)
git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs git push --delete origin

Note:  each line of output from "git branch --remote" will begin with " origin/" and so the "cut -b 10-" removes that.  If your remote is named something other than "origin" you will need to adjust the value "10" in that cut command.

The problem with this approach is that sometimes important branches like 'develop' or 'production' or 'release' might have been merged without any subsequent activity, and so they will get toasted by this.

I recommend installing my free add-on first (Commit Graph for Bitbucket Server) and reviewing all the merged branches first before running the delete.  It's a great way to visually confirm that you're not deleting anything important.  Just uncheck all the options except "master" on the "All Branches Graph", hit reload, and scroll down to visually inspect the branches before deleting.

Here's a live demo of the "All Branches Graph" in action with only 'master' checked:

Any branches that you want to keep you can remove from the command above with "grep -v" additions after the initial "grep -v master" component (e.g.,  grep -v 'master' | grep -v 'keep-this' | grep -v 'and-keep-this-too' | etc...).



I don't see the difference between the first and second line:

# Initial no-op run --- do the branches to delete look correct?
# Be careful to omit 'master' from the output.
git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs

# Do the bulk delete!!!  (can take a long time...)
git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs

Neither can Chrome in Ctrl+F. Can you help me how is one no-op run and the other the actual action? 

Like Antoine Purnelle likes this


Replace " | xargs " with  " | xargs git push --delete origin ".


Note: xargs by itself just prints the lines fed into it, whereas "xargs <cmd>" takes all the lines fed into xargs and runs <cmd> with all the lines as arguments to <cmd>.

Like # people like this

I case you don't want to install any add-ons from marketplace you have another option.

I've wrote this small script when the number of branches in my repo exceeded several hundreds. I did not know about the first method so I decided to automate it with selenium. If this fits for you, you can try that way.

All you need is to clone this repository, download the proper version of Chrome-webdriver, input few constants like URL to your own repository and run the script.

The code is simple enough to understand. If you have any questions, write comments / create an Issue.

Best way is to sense developers to delete their branches immediately after merge if all work for that branch is done. The reviewer may check this. Make this to your QA process.

To clean up your current branches, the plugin "My Branches" may help. It determines your branch contributions, lists them, and lets you delete outdated branches easily. You can delete multiple branches in bulk.

If you have any questions about that this plugin, don't hesitate to contact me.

0 votes

i have a shell script which will delete all the branches that has no commits in the past 60 days, this was implemented as part for gitclean up activity.


After purging these branches in git the first generator job takes time approx 2-3 hours due to the references of the deleted branches in bitbucket. Do you know a way to get rid of these references when the git branches are being purged ?

Deleted user Jul 15, 2019

i get this error in jenkins post branch purge/delete


19:48:09 Deleting unreferenced Branch folder:

 this approx takes 3 mins to delete one branch,, our project has 30 branches deleted approx which takes hours for this job. Pleas help !!! 

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events