Clean up your Bitbucket Data Center instance

Still need help?

The Atlassian Community is here for you.

Ask the community

Support for Server licenses ended on February 15, 2024. Discover your options.

The purpose of this guide is to give you direction and outline the ways in which you can declutter your Bitbucket instance. 

There is no single approach that will work in all situations, so on this page, we'll show a range of techniques you can use to clean up your instance. This will no doubt be a team effort so you'll want to enlist the help of people in your organization. See Prepare for the cleanup for more about determining business rules and getting the right people involved.  

It's a good idea to back up your data before you start. See how to create a backup. We also recommend making changes in your test environment first before applying them in production.

Delete inactive users

Whenever someone leaves your organization, it’s good practice to delete their user account. This keeps your instance safe, keeps people from accidentally assigning pull requests to an inactive user, and of course, preserves your license capacity.

Here are three ways to help finding and deleting inactive users:

How do I automate deleting users?

You can delete users via REST API. For example:

curl -u USERNAME:PASSWORD -v -X DELETE 'http://localhost:7990/bitbucket/rest/api/latest/admin/users?name={USER_SLUG}'

Marketplace apps

Delete merged branches

Over time, your instance can become cluttered with merged branches that could safely be deleted.

The Branches page in Bitbucket allows you to filter branches, review their status, and then delete them, one at a time. For more information, see Using branches in Bitbucket Server.

How do I automate deleting branches?

It’s possible to delete all merged branches using Git command-line. For example, to delete all branches already merged into the main branch:

#Ensure that refs are up to date, and that stale local refs are pruned.
git fetch -p

#Dry run to list branches which will be deleted on the remote.
#This assumes that the remote is named 'origin'.
#Note that branches can be excluded from deletion by adding them after
#the 'main' branch exclusion, e.g. "grep -v 'main' | grep -v 'branch-to-keep'"
git branch --remote --merged origin/main | grep -v 'main' | sed 's/origin\///'

#Delete the branches on the origin. This process can take a long time to run
#and will use up SCM resources on Bitbucket. It's recommended to run it during
#downtime, and possibly in batches.
git branch --remote --merged origin/master | grep -v 'main' | sed 's/origin\///' | xargs git push --delete origin


Marketplace apps

Delete inactive repositories

Workaround prior to Bitbucket 7.13

If your Bitbucket instance is several years old, you’re bound to have personal repositories of deleted users, or repositories that haven’t been accessed for months.

This workaround on how to find personal repositories of deleted users helps you find repositories and then you can decide whether you want to move or delete them. 

How do I automate deleting repositories?

Deleting repositories can be automated via the REST API. For example:

curl -u USERNAME:PASSWORD -v -X DELETE http://localhost:7990/bitbucket/rest/api/latest/projects/{PROJECT_KEY}/repos/{REPO_SLUG}

Marketplace apps

You can use ScriptRunner to remove all repositories for a particular project. Please see this example to delete all repositories in a project, which can be run in Admin > ScriptRunner > Script Console. Learn more about ScriptRunner 

This feature is available with a Bitbucket Data Center license.

Routine repository clean-up is most likely necessary for your Bitbucket Data Center instance, and the Repositories page helps you easily find and clean up inactive repositories that are causing clutter.

Find out more from our Advanced repository management documentation to help you avoid the repetitive task of deleting repositories one at a time, by locating and deleting them all at once.

Delete old Git tags

Having a large number of tags in a repository increases its size and can make clone or fetch operations run longer.

You can view all tags associated with a particular commit by navigating to it on the Commits page. Once there, you can delete tags if you have write access to the repository.

How do I automate deleting tags?

You can delete all tags on the remote, excluding those containing a pattern, for example:

#Dry run to list tags which will be deleted on the remote.
#Note that tags can be excluded from deletion by adding them after as
#inverted grep matches, e.g. "grep -v 'release' | grep -v 'tag-to-keep'".
git ls-remote --tags | grep -v '\^{}$' | grep -v 'release' | cut -f2 -d$'\t' | xargs -n 1

#Delete the tags on the origin. This process can take a long time to run
#and will use up SCM resources on Bitbucket. It's recommended to run it during
#downtime, and possibly in batches.
git ls-remote --tags | grep -v '\^{}$' | grep -v 'release' | cut -f2 -d$'\t' | xargs -I % git push origin :%

Automatically decline inactive pull requests

This feature is available with a Bitbucket Data Center license.

As the number of open pull requests in your list grows, it brings unwanted clutter. Using the Auto-decline pull requests option will increase productivity and optimize performance of your Bitbucket Data Center instance by automatically declining inactive pull requests. This option is on by default and is set on individual repositories or all in a project.

For more information on how to use this feature, head over to Automatically decline pull requests.

Last modified on May 15, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.