How do I delete a bitbucket file using REST api?

There is documentation for update and create new file into bitbucket.
Is there a delete REST api call ?

Thanks,

1 Like

Jenli,
Bitbucket server currently does not support deleting files using REST APIs. Feel free to open a improvement/suggestion.

Thanks,
Justin

1 Like

Im very new to the subject and i want to connect and retrieve some basic info using bb rest api via my java app…
And i dont know how to write the java app can you please point or notify any example on the internet if possible…
I searched for the topic and wht i actually found is developing add on service to bb…
Thanks a lot

@jthomas From src API Documentation:

files string
Optional field that declares the files that the request is manipulating. When adding a new file to a repo, or when overwriting an existing file, the client can just upload the full contents of the file in a normal form field and the use of this files meta data field is redundant. However, when the files field contains a file path that does not have a corresponding, identically-named form field, then Bitbucket interprets that as the client wanting to replace the named file with the null set and the file is deleted instead.

Is this a new feature since your reply? I’m currently trying to test this but having trouble getting it to work since there is no indication on how to delimit the files (comma? semicolon?)

1 Like

I also could not figure out how to separate files. Providing just one file works. So one workaround could be to commit deletion of each single file …

To delete a file you would pass in the name of the file you’d like to delete as one of the files parameters and not pass it in as its own parameter. It might make more sense how to delete a file if I first also explain how to add multiple files.

For each file that you’d like to add or edit, you pass in the file contents with a form parameter where the parameter name is the full path of the file, relative to the repository.

Additionally, you can pass each file name as a value to the files parameter. In the following example setting the files parameter isn’t necessary–this parameter is only useful if you’d like to delete a file. In this example nothing will be deleted because each of the files parameters also has its own parameter.

curl -v https://api.bitbucket.org/2.0/repositories/example-username/repo-id/src \
  -F /beatles/john.txt=john \
  -F /beatles/paul.txt=paul \
  -F /beatles/ringo.txt=ringo \
  -F /beatles/george.txt=george \
  -F files=beatles/john.txt \
  -F files=beatles/paul.txt \
  -F files=beatles/ringo.txt \
  -F files=beatles/george.txt 

If you’d like to delete a file, then you’d do the following. Send the files to be deleted as a files parameter without sending the contents:

curl -v https://api.bitbucket.org/2.0/repositories/example-username/repo-id/src \
  -F files=beatles/john.txt \
  -F files=beatles/george.txt

After running these two commands, you’d have 2 more commits in your history and 2 files in your /beatles directory:

beatles/paul.txt
beatles/ringo.txt

File contents can either be passed in either as Content-Type: multipart/form-data like this example or as Content-Type: application/x-www-form-urlencoded. See this page for more detail: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/{username}/{repo_slug}/src#post

There are some gotchas around if the name of your file collides with one of the parameters for setting metadata (e.g. message, author) and this page details how you can account for those scenarios: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/{username}/{repo_slug}/src#post

1 Like

Thanks that worked. I didn’t get the documentation, might be to my mindset. I thought there is one field “files” where multiple file names should be submitted at once. But the field should appear multiple times for each file name.

That did the trick.

I’ve seen different places handle passing lists different ways. If you’ve never encountered it before, it might seem counter-intuitive that you can pass in list of parameters by duplicating the name. But this is how a native HTML <select multiple> works when you select multiple and submit it.

From the HTML Living Standard 4.10.21.4 Constructing the entry list:

If the field element is a select element, then for each option element in the select element’s list of options whose selectedness is true and that is not disabled, append an entry to entry list with name and the value of the option element.

If someone is not aware that there is already a standard for passing in a list of values they might construct their own mechanism by submitting a list of values as one parameter but separated by a comma. This however is unnecessary.

Is there a way to delete files for a date range using the API?