Clean up your Bitbucket Data Center instance
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:
To get a list of users showing their last login in, see Administration > Users. From the results on that page, you can delete users.
If you use Atlassian Crowd, the Centralized license visibility app can also help you monitor license usage. For details, se Monitoring license usage docs.
Alternatively, you can check out this article with details on users last login through the REST API or from SQL.
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
User management for Bitbucket can help you identify users with configurable criteria and set a schedule to automatically run actions.
User deactivator for Bitbucket helps keep your user directory clean of non-active users.
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
My Branches for Bitbucket determines your branch contributions, lists them, and lets you delete outdated branches easily.
Mass Delete branches from Bitbucket adds a bulk delete option to the branch page so that you can select multiple branches and delete them all at once.
Delete inactive repositories
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.