Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

Delete stale branches of bitbucket repositories

Hi i wanted to delete the branches which are not in use since last six months. I wan to list it and delete it any suggestions

2 answers

0 votes
Caroline R Atlassian Team Dec 29, 2021

Hi, @Shaik Moulali! Welcome to the community! 

We currently don't have an option to bulk delete stale branches through UI nor through CLI. 

However, you can create a bash script locally to bulk delete those branches. We have a couple of suggestions that you can follow to achieve your goal: 

  1. If you have a list of branches that you can delete, you can run a setup bash script to run 'git push origin --delete <branch-name>' to delete the remote branch
  2. If you would like to get a list of branches with the last commit date, the following commands will help: 
    git clone --mirror <Repo-URL>
    cd <Repo-name>
    git for-each-ref --sort=committerdate refs/heads/ --format='(%(color:green)%(committerdate:relative)%(color:reset))  %(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname)'

    There's a sample script that you can use to bulk delete old branches:

  • We've modified the script if you are using mirror repo: 
    # Script to delete remote git branches
    # Fetch the remote resources
    git fetch
    # Loop through all remote merged branches
    for branch in $(git branch --merged | grep -v HEAD | grep -v develop | grep -v master | grep -v master | sed /\*/d); do
            if [ -z "$(git log -1 --since='Jun 15, 2020' -s ${branch})" ]; then
                    echo -e `git show --format="%ci %cr %an" ${branch} | head -n 1` \\t$branch
                    remote_branch=$(echo ${branch} | sed 's#origin/##' )
                    # To delete the branches uncomment the bellow git delete command
                    # git branch -D ${remote_branch}
  • As described on the community page:
    • You can delete the `--merged` flag so that the script would delete all branches no matter if merged or not
    • The --since='Jun 15, 2020' date indicates the date since the branch has been last worked on. The above example will delete all branches that have been idle (eg. no commits or any other activity) since Jun 15, 2020. Update it with the date that matches your needs.
  • Once the script is completed, you can run 'git push origin' to push the latest changes back to Bitbucket

With that said, we also would like to share with you that we already have a feature request for our dev team to allow users to set a retention period for deleting unused branches. You can access this request here: 

I've linked this question to the feature. Please consider adding yourself as a watcher. This way you get updated as we make progress with this. If you are not familiar with our Feature Request Policy, you can read more about it here.

I hope this helps, but do let me know if you have any questions.

Kind regards,

0 votes

There are really good ideas in this topic:

I think that a good definition of a branch to delete would be a branch that has its head older than N days and not merged to master. The first one alone is not enough. Also you may want to delete feature/* and bugfix/* only, not everything. Be careful! 

(You should be able to modify those one-line Linux commands to delete branches remotely.)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

⭐ Calling all Bitbucket and DevOps experts: Special showcase opportunity ⭐

Hi, Bitbucket community! Are you a DevOps practitioner (or know one in your network)? Do you have DevOps tips, tricks, or learnings you'd like to share with the community? If so, we'd love to hea...

1,527 views 4 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