Search code examples
gitbitbucketbitbucket-api

How to delete a branch using Bitbucket REST API


Using postman, I have succeeded in creating branches in bitbucket via their REST API and using the guide below

Bitbucket Rest API Guide

But I cannot delete a branch using the same guide as well.

I have created a JSON and placed it in the body tab of POSTMAN then using DELETE for the HTTP method but no success.

I am getting error 405 - Method Not Allowed, what does that mean? Did my request pushed through but I am not allowed?

Using the bitbucket web UI, I am able to delete and create branches.

Edit:

This is the postman generated CURL

curl -X DELETE https://<url>/rest/api/1.0/projects/<abc>/repos/<xyz>/branches 
-H 'authorization: Bearer xxxxxx' -H 'cache-control: no-cache' 
-H 'content-type: application/json'  
-H 'x-atlassian-token: nocheck' 
-d '{"name": "refs/heads/feature/name","dryRun": false}'

Solution

  • It looks like you're using the incorrect REST endpoint, one that doesn't accept the DELETE HTTP verb. The one you're using is:

    /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/branches
    

    As per the docs, this endpoint only accepts GETs and POSTs

    Judging by the format of your call, I'm guessing the one you're actually after is this:

    /rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches
    

    The branch-utils API docs describe more or less the exact payload you're trying to use.

    Digging a little deeper and I believe this mistake isn't your fault. The Bitbucket branch util docs for Bitbucket 5.8 and below show the correct URL path, but for 5.9 and up the path seems to be missing.

    Full disclosure: I work for Atlassian. That being the case I'll chase this up and have it corrected :)

    Edit: the docs have since been fixed!