How to identify a deleted branch
Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.
Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
Identify accidentally deleted branches.
Solution
SQL Query
Deleted branches can be detected by running the following query in Bitbucket Server:
Replace <project_key> and <repository_slug> with the appropriate values:
1 2 3 4 5 6 7 8 9 10 11
select p.project_key, r.slug, pr.ref_id, pr.change_type, pr.from_hash, pr.to_hash, nu.name, a.created_timestamp from sta_repo_push_ref pr join sta_repo_activity ra on ra.activity_id = pr.activity_id join repository r on r.id = ra.repository_id join project p on p.id = r.project_id join sta_activity a on a.id = pr.activity_id join sta_normal_user nu on nu.user_id = a.user_id where p.project_key = '<project_key>' and r.slug = '<repository_slug>' and to_hash like '0000000%' order by a.created_timestamp desc;
The query checks for branches where the tip of the branch is 0000000000000000000000000000000000000000. This identifies a deleted branch.

The <project_key> is the key defined for the project in the project table, and the <repository_slug> is the repo slug defined in the repository table.
REST API
From Bitbucket 7.1.0 onwards, the GET /rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/ref-change-activities endpoint can be used to retrieve changes to repo refs, which include branch deletions.
Sample API output for a deleted branch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
...
{
"id": 6477,
"createdDate": 1680049511420,
"user": {
"name": "user1",
"emailAddress": "user1@atlassian.com",
"id": 2,
"displayName": "user1",
"active": true,
"slug": "user1",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://bitbucket.test.com/users/user1"
}
]
}
},
"repository": {
"slug": "repo1",
"id": 1,
"name": "repo1",
"hierarchyId": "374f1d313b9ccec62c49",
"scmId": "git",
"state": "AVAILABLE",
"statusMessage": "Available",
"forkable": true,
"project": {
"key": "PROJ1",
"id": 1,
"name": "Project 1",
"public": false,
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://bitbucket.test.com/projects/PROJ1"
}
]
}
},
"public": true,
"links": {
"clone": [
{
"href": "ssh://git@bitbucket.test.com/proj1/repo1.git",
"name": "ssh"
},
{
"href": "http://bitbucket.test.com/scm/proj1/repo1.git",
"name": "http"
}
],
"self": [
{
"href": "http://bitbucket.test.com/projects/PROJ1/repos/repo1/browse"
}
]
}
},
"trigger": "branch-delete",
"refChange": {
"ref": {
"id": "refs/heads/cd",
"displayId": "cd",
"type": "BRANCH"
},
"refId": "refs/heads/cd",
"fromHash": "de4f52fa7859d53c70733fc748df29689f713dc9",
"toHash": "0000000000000000000000000000000000000000",
"type": "DELETE",
"updateType": "NOT_FORCED"
}
}
...
the
refChange.type
value is "DELETE
"the
refChange.toHash
value shows "0000000000000000000000000000000000000000"
Restore a deleted branch
Once the deleted branches have been identified, if the branch has to be restored, follow the instructions on the How to restore a deleted branch page.
Bitbucket versions earlier than 7.20 do not log branch create and delete events performed through the UI in the audit logs.
This has been addressed from version 7.20 onwards, as part of the feature request, BSERV-7046 - Log branch addition and deletion made via Stash UI to the Audit logs.
Was this helpful?