Is there a way to delete branches in bulk?

Joey Vargas August 29, 2016

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

31 votes
Answer accepted
G__Sylvie_Davies__bit-booster_com_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 29, 2016

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:  vm.bit-booster.com/bitbucket

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...).

 

 

David Tremblay-Fafard April 15, 2020

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
G__Sylvie_Davies__bit-booster_com_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2020

 

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
3 votes
D G June 11, 2019

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.


https://github.com/globad/remove-old-branches

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.

1 vote
Johannes Heger (CD) August 29, 2016

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.

https://marketplace.atlassian.com/plugins/jh.atlassian.stash.mybranches.stash-mybranches/server/overview

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

0 votes
Deleted user July 15, 2019

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 July 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
TAGS
AUG Leaders

Atlassian Community Events