Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
badges earned

Your Points Tracker
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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.

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 ?

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
Community showcase
Published in Bitbucket Pipelines

Bitbucket Pipelines Runners is now in open beta

We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...

518 views 10 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you