Error Deleting Remote Git Branch via Azure DevOps API: Unsupported HTTP Method 'POST'

67 views Asked by At

I'm encountering an issue while attempting to delete a remote Git branch using the Azure DevOps REST API. Despite verifying permissions and trying both Personal Access Token (PAT) and Bearer token authentication methods, I consistently receive an error indicating that the requested resource does not support the HTTP method 'POST'. Below is the detailed code snippet I'm using along with additional context:

Code Snippet:

curl -X POST -u username:token -H "Content-Type: application/json" -d '{"refUpdates":[{"name":"refs/heads/{branchName}","oldObjectId":"{objectId}"}]}' "https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes?api-version=6.0-preview.1"

Error Message:

{"count":1,"value":{"Message":"The requested resource does not support http method 'POST'."}}

Additional Details:

  1. Authentication Methods Tried: I have attempted both PAT and Bearer token authentication methods to authenticate the API call.

  2. Permissions Verified: I have verified that the token used for authentication has full access permissions.

  3. API Endpoint and Version: The API call is targeting the appropriate endpoint /_apis/git/repositories/{repositoryId}/pushes with the correct version api-version=6.0-preview.1.

  4. Objective: My objective is to delete a remote Git branch programmatically using the Azure DevOps API.

I have thoroughly reviewed the Azure DevOps API documentation and ensured that I'm following the correct procedure, yet the issue persists. Any insights or suggestions on how to resolve this issue would be greatly appreciated. Thank you!

1

There are 1 answers

1
Bright Ran-MSFT On

The API "Pushes - Create" cannot be used to delete branch from Azure Git Repos, instead, you should use the API "Refs - Update Refs".

Below is a Bash script to delete branch from Azure Git Repos using curl command to call the related API.

#!/bin/bash

organization="{organization}"
project="{project}"
repository="{repository}"
branchName="{branchName}"  # e.g., refs/heads/develop
pat="{pat}"
uri="https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?api-version=7.0"

# Get the objectId of the branch.
objectId=$(curl -X GET -u :$pat $uri | jq -r --arg branchName $branchName '.value[] | select(.name==$branchName) | .objectId')
echo "objectId = $objectId"

# Delete the branch.
curl -X POST -u :$pat $uri \
-H "Content-Type: application/json" \
-d "[
    {
        'name': '$branchName',
        'newObjectId': '0000000000000000000000000000000000000000',
        'oldObjectId': '$objectId'
    }
]"