b2_delete_file_version

Deletes one version of a file from B2.

If the version you delete is the latest version, and there are older versions, then the most recent older version will become the current version, and be the one that you'll get when downloading by name. See the File Versions page for more details.

When used on an unfinished large file, this call has the same effect as b2_cancel_large_file.

Request

Request HTTP Headers

Authorization

required

The account authorization token returned by b2_authorize_account.

Request HTTP Message Body Parameters

fileName

required

The name of the file.

fileId

required

The ID of the file, as returned by b2_upload_file, b2_list_file_names, or b2_list_file_versions.

Response

Response HTTP Status 200

File version successfully deleted. The JSON response will contain:

fileId

The unique ID of the file version that was deleted.

fileName

The name of the file.

Response Errors

File version not deleted. If possible the server will return a JSON error structure. Errors include:

status

code

message

400

bad_bucket_id

Bucket <bucketId> does not exist

400

bad_request

Snapshot buckets are reserved for Backblaze use

400

bad_request

Allow snapshot header must be specified when deleting a file from a snapshot bucket

400

file_not_present

File not present: <fileName> <fileId>

400

bad_request

Wrong host name for request

401

missing_auth_token

Authorization token is missing

401

expired_auth_token

Authorization token has expired

401

bad_auth_token

Invalid authorization token

401

bad_auth_token

Authorization token for wrong cluster

Sample Code

Code

curl \
    -H "Authorization: ACCOUNT_AUTHORIZATION_TOKEN" \
    -d '{"fileName": "FILE_NAME", "fileId": "FILE_ID"}' \
    https://apiNN.backblaze.com/b2api/v1/b2_delete_file_version

Output

{
    "fileId" : "4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104",
    "fileName" : "typing_test.txt"
}

Code

import java.io.*;
import java.util.*;
import javax.json.*;
import java.net.HttpURLConnection;
import java.net.URL;

String apiUrl = ""; // Provided by b2_authorize_account
String fileId = "";  // The fileId of the file you want to delete
String fileName = ""; // Name of the file you want to delete
String accountAuthorizationToken = ""; // Provided by b2_authorize_account
HttpURLConnection connection = null;
byte postData[] = "{\"fileId\":\"" + fileId + "\",\"fileName\":\"" + fileName + "\"}".getBytes(StandardCharsets.UTF_8);
try {
    URL url = new URL(apiUrl + "/b2api/v1/b2_delete_file_version");
    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Authorization", accountAuthorizationToken);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
    connection.setDoOutput(true);
    DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
    writer.write(postData);
    String responsJson = myInputStreamReader(connection.getInputStream());
    System.out.println(jsonResponse);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    connection.disconnect();
}

static public String myInputStreamReader(InputStream in) throws IOException {
    InputStreamReader reader = new InputStreamReader(in);
    StringBuilder sb = new StringBuilder();
    int c = reader.read();
    while (c != -1) {
        sb.append((char)c);
        c = reader.read();
    }
    reader.close();
    return sb.toString();
}

Output

{
    "fileId" : "4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104",
    "fileName" : "typing_test.txt"
}

Code

import json
import urllib2

api_url = "" # Provided by b2_authorize_account
account_authorization_token = "" # Provided by b2_authorize_account
file_name = "" # The name of the file you want to delete
file_id = "" # The fileId of the file you want to delete

request = urllib2.Request(
    '%s/b2api/v1/b2_delete_file_version' % api_url,
    json.dumps({ 'fileName' : file_name, 'fileId' : file_id }),
    headers = { 'Authorization': account_authorization_token }
    )
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
response.close()

Output

{
    "fileId" : "4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104",
    "fileName" : "typing_test.txt"
}

Code

import Foundation

apiUrl = "" // Provided by b2_authorize_account
accountAuthorizationToken = "" // Provided by b2_authorize_account
fileName = "" // The name of the file you want to delete
fileId = "" // The fileId of the file you want to delete
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url.URLByAppendingPathComponent("/b2api/v1/b2_delete_file_version"))
request.HTTPMethod = "POST"
request.addValue(accountAuthorizationToken, forHTTPHeaderField: "Authorization")
request.HTTPBody = "{\"fileName\":\"\(fileName)\",\"fileId\":\"\(fileId)\"}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let task = session.dataTaskWithRequest(request, completionHandler:{ (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
	if data != nil {
        let json = NSString(data: data, encoding:NSUTF8StringEncoding)
    }
})
task.resume()

Output

{
    "fileId" : "4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104",
    "fileName" : "typing_test.txt"
}

Code

require 'json'
require 'net/http'

api_url = "" # Provided by b2_authorize_account
account_authorization_token = "" # Provided by b2_authorize_account
file_name = "" # The name of the file you want to delete
file_id = "" # The fileId of the file you want to delete
uri = URI("#{api_url}/b2api/v1/b2_delete_file_version")
req = Net::HTTP::Post.new(uri)
req.add_field("Authorization","#{account_authorization_token}")
req.body = "{\"fileName\":\"\(file_name)\", \"fileId\":\"\(file_id)\"}"
http = Net::HTTP.new(req.uri.host, req.uri.port)
http.use_ssl = true
res = http.start {|http| http.request(req)}
case res
when Net::HTTPSuccess then
    res.body
when Net::HTTPRedirection then
    fetch(res['location'], limit - 1)
else
    res.error!
end
    

Output

{
    "fileId" : "4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104",
    "fileName" : "typing_test.txt"
}

Code

String apiUrl = "API_URL"; //Provided by b2_authorize_account
String accountAuthorizationToken = "ACCOUNT_AUTHORIZATION_TOKEN"; //Provided by b2_authorize_account
String fileName = "FILE_NAME"; //The name of the file
String fileId = "FILE_ID"; //The unique file ID
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_delete_file_version");
string body =
   "{\"fileName\":\"" + fileName + "\",\n" +
   "\"fileId\":\"" + fileId + "\"}";
var data = Encoding.UTF8.GetBytes(body);
webRequest.Method = "POST";
webRequest.Headers.Add("Authorization", accountAuthorizationToken);
webRequest.ContentType = "application/json; charset=utf-8";
webRequest.ContentLength = data.Length;
using (var stream = webRequest.GetRequestStream())
{
	stream.Write(data, 0, data.Length);
	stream.Close();
}
WebResponse response = (HttpWebResponse)webRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
Console.WriteLine(responseString);

Output

{
    "fileId" : "4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104",
    "fileName" : "typing_test.txt"
}

Code

$api_url = ""; // From b2_authorize_account call
$auth_token = ""; // From b2_authorize_account call
$file_id = "";  // The ID of the file you want to delete
$file_name = "file.txt"; // The file name of the file you want to delete

$session = curl_init($api_url .  "/b2api/v1/b2_delete_file_version");

// Add post fields
$data = array("fileId" => $file_id, "fileName" => $file_name);
$post_fields = json_encode($data);
curl_setopt($session, CURLOPT_POSTFIELDS, $post_fields); 

// Add headers
$headers = array();
$headers[] = "Authorization: " . $auth_token;
curl_setopt($session, CURLOPT_HTTPHEADER, $headers); 

curl_setopt($session, CURLOPT_POST, true); // HTTP POST
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);  // Receive server response
$server_output = curl_exec($session); // Let's do this!
curl_close ($session); // Clean up
echo ($server_output); // Tell me about the rabbits, George!

Output

{
    "fileId" : "4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104",
    "fileName" : "typing_test.txt"
}