How to: API to delete branches on Azure DevOps
Use this automation to delete old branches within your Azure DevOps organization
There is frequently a necessity to remove our feature branches to maintain our repositories as tidy as possible, guaranteeing that only functional branches are displayed. As our code progresses, we frequently overlook carrying out this deletion (even for branches that have already been merged with main branches), resulting in a collection of branches that have already concluded their lifecycle.
To streamline this cleaning process, I’ve created a script that handles this deletion, ensuring our branch list remains as neat as possible. It’s important to note that any deleted branch can be restored at any time since it receives a soft delete marker. Further instructions on branch recovery are available in the documentation.
Now, let’s undestand how the script operates. This same script example is available in my GitHub repository.
Param
(
[string]$PAT,
[string]$Organization,
[string]$ProjectName,
[string]$RepositoryName,
[string]$BranchName
)
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$UriOrganization = "https://dev.azure.com/$($Organization)/"
$uriRepositories = "$($UriOrganization)$($ProjectName)/_apis/git/repositories/$($RepositoryName)?api-version=7.0"
$RepositoriesResult = Invoke-RestMethod -Uri $uriRepositories -Method get -Headers $AzureDevOpsAuthenicationHeader
if ($RepositoriesResult)
{
$uribranchExists="$($UriOrganization)_apis/git/repositories/$($RepositoriesResult.id)/refs"
$branchExistsResults = Invoke-RestMethod -Uri $uribranchExists -Method get -Headers $AzureDevOpsAuthenicationHeader
$validBranch = $branchExistsResults.value | where-object {$_.name -eq "refs/heads/$($BranchName)"}
if ($validBranch)
{
$body = ConvertTo-Json (
@(
@{
name = $validBranch.name;
oldObjectId = $validBranch.objectId;
newObjectId = "0000000000000000000000000000000000000000";
}
)
)
$urlDeleteBranch = "$($UriOrganization)$($ProjectName)/_apis/git/repositories/$($RepositoriesResult.id)/refs?api-version=7.1-preview.1"
$DeleteBranchResult = Invoke-RestMethod -Uri $urlDeleteBranch -Method Post -Headers $AzureDevOpsAuthenicationHeader -Body $body -ContentType "application/json"
}
}
You need to provide the following parameters:
- PAT = Personal Access Token;
- Organization = the name of the organization;
- ProjectName = the name of the project;
- RepositoryName = the name of the repository that contains a branch;
- BranchName = The name of the branch that will be deleted.
The script performs the following validations:
- Checks the existence of the repository passed as a parameter;
- Lists all branches in the repository and filters the branch name passed as a parameter.
I can execute the call in PowerShell by passing the parameters. In the example below, I will delete the branch called feature2.
After executing the script, the branch no longer exists and can be found in the deleted branches section where it can be recovered.