How To Delete Repository In Bitbucket

Deleting a repository in Bitbucket is a permanent action. Ensure you have backups of your code and understand the implications before proceeding. This guide will walk you through the steps to delete a Bitbucket repository and offers practical considerations for your daily workflow.
Deleting a Repository (with Admin Access)
If you have administrator permissions for the repository, you can delete it directly from the Bitbucket settings.
Step 1: Navigate to Repository Settings
Must Read
Go to your Bitbucket repository. In the left-hand sidebar, find and click on "Repository settings".
Step 2: Access Delete Repository Option
Within Repository settings, locate the "Delete repository" option. This is usually found at the bottom of the settings page, often within a "Danger Zone" or similar section.
Step 3: Confirm Deletion
Click on the "Delete repository" button. Bitbucket will present a confirmation dialog. You will likely need to type the repository name to confirm your intent. This is a safeguard to prevent accidental deletion. Carefully type the name and then click "Delete repository" again.
Step 4: Repository Deletion

The repository will now be permanently deleted. This process might take a few seconds. Afterward, you'll be redirected, often to your Bitbucket account dashboard.
Using the Bitbucket API to Delete a Repository
For automated workflows or when managing multiple repositories, you can use the Bitbucket API to delete repositories.
Step 1: Generate an App Password
To use the API, you'll need an app password with the necessary permissions. In Bitbucket, navigate to your profile settings (usually by clicking your avatar in the top right corner). Then, find "Personal settings" and then "App passwords". Create a new app password. Give it a descriptive name (e.g., "Repository Deletion Script"). Grant the app password "repository:admin" permission. Store this app password securely.
Step 2: Construct the API Request
The API endpoint for deleting a repository is:

DELETE /2.0/repositories/{workspace}/{repo_slug}
Replace {workspace} with your Bitbucket workspace ID (usually your username or team name) and {repo_slug} with the repository slug (the repository name as it appears in the URL).
Step 3: Send the API Request
You can use tools like curl, Postman, or any scripting language with HTTP client libraries to send the request. Here's an example using curl:
curl -X DELETE \
-u "your_username:your_app_password" \
"https://api.bitbucket.org/2.0/repositories/your_workspace/your_repo_slug"
Replace your_username with your Bitbucket username, your_app_password with the app password you generated, your_workspace with your workspace ID, and your_repo_slug with the repository slug.
Step 4: Verify Deletion
A successful deletion will return a 204 No Content HTTP status code. You can also verify the deletion by trying to access the repository in the Bitbucket web interface; it should return a 404 Not Found error.

Practical Considerations for Daily Life/Work
1. Backups are Crucial: Before deleting any repository, ensure you have a complete and up-to-date backup of your code. This could be a local copy, a mirror repository on another platform (like GitHub), or a backup stored in a cloud service.
2. Communication is Key: If you're working in a team, communicate your intention to delete a repository well in advance. Give team members ample time to back up their work or adjust their workflows. Announce the deletion and the reason for it. Nobody likes surprises!
3. Consider Alternatives to Deletion: Instead of deleting a repository, consider archiving it. Bitbucket doesn't directly offer an "archive" feature, but you can achieve a similar effect by:
- Making the repository private (if it isn't already).
- Removing all write access for team members except for a designated administrator.
- Adding a prominent notice to the repository's README file indicating that it's archived and no longer actively maintained.
This approach preserves the repository's history and code for future reference while preventing further development.
4. Branch Management: Ensure all active branches are merged or properly handled before deleting. Deleting a repository will delete all branches associated with it.
5. Automated Deletion Workflows: When automating repository deletion using the API, implement robust error handling and logging. Proper logging will help you track which repositories were deleted, when, and by whom. Error handling will prevent unexpected failures and ensure that the deletion process is reliable.

6. Permissions and Security: Regularly review and manage user permissions in your Bitbucket workspace. Ensure that only authorized individuals have the necessary permissions to delete repositories. Use app passwords with the least privilege necessary for automated tasks.
7. Impact on Integrations: Deleting a repository can break integrations with other tools, such as CI/CD pipelines, issue trackers, and deployment systems. Identify and update these integrations before deleting the repository.
8. Legal and Compliance: Consider any legal or compliance requirements related to your code. Some organizations are required to retain code for a specific period, even if it's no longer actively used. Consult with legal or compliance teams before deleting repositories that may be subject to such requirements.
9. Documentation: Maintain clear documentation about your repository management processes. This documentation should include guidelines for deleting repositories, archiving repositories, and backing up code.
Deletion Checklist
Before deleting a Bitbucket repository, review this checklist:
- Backup: Create a full backup of the repository's code and history.
- Communication: Inform team members about the impending deletion.
- Alternatives: Consider archiving the repository instead of deleting it.
- Branches: Merge or handle all active branches.
- Integrations: Update or remove integrations with other tools.
- Permissions: Verify that you have the necessary permissions.
- Legal/Compliance: Ensure deletion complies with legal and compliance requirements.
- Documentation: Update repository management documentation as needed.
- Confirmation: Double-check the repository name before confirming deletion.
