b2_delete_key

Deletes the application key specified.

Request

Request HTTP Headers

Authorization

required

An account authorization token, obtained from b2_authorize_account. The token must have the deleteKeys capability.

Request HTTP Message Body Parameters

applicationKeyId

required

The key to delete.

Response

Response HTTP Status 200

Key successfully deleted. The JSON response will contain:

keyName

required

The name assigned when the key was created.

applicationKeyId

required

The ID of the newly created key.

capabilities

required

A list of strings, each one naming a capability the key has. Possibilities are: listKeys, writeKeys, deleteKeys, listAllBucketNames, listBuckets, readBuckets, writeBuckets, deleteBuckets, readBucketRetentions, writeBucketRetentions, readBucketEncryption, writeBucketEncryption, listFiles, readFiles, shareFiles, writeFiles, deleteFiles, readFileLegalHolds, writeFileLegalHolds, readFileRetentions, writeFileRetentions, bypassGovernance, readBucketReplications, and writeBucketReplications.

accountId

required

The account that this application key is for.

expirationTimestamp

optional

When present, says when this key will expire, in milliseconds since 1970.

bucketId

optional

When present, restricts access to one bucket.

namePrefix

optional

When present, restricts access to files whose names start with the prefix.

options

optional

When present and set to s3, the key can be used to sign requests to the S3 Compatible API.

Response Errors

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

status

code

description

400

bad_bucket_id

The requested bucket ID does not match an existing bucket.

400

bad_request

The request had the wrong fields or illegal values. The message returned with the error will describe the problem.

400

cannot_delete_non_empty_bucket

A bucket must be empty before it can be deleted. To delete this bucket, first remove all of the files in the bucket, then try the delete operation again.

401

bad_auth_token

The auth token used is not valid. Call b2_authorize_account again to either get a new one, or an error message describing the problem.

401

expired_auth_token

The auth token used has expired. Call b2_authorize_account again to get a new one.

401

unauthorized

The auth token used is valid, but does not authorize this call with these parameters. The capabilities of an auth token are determined by the application key used with b2_authorize_account.

Sample Code

Code

ACCOUNT_AUTHORIZATION_TOKEN = "" # Provided by b2_authorize_account
API_URL = "" # Provided by b2_authorize_account
APPLICATION_KEY_ID = "" # The key to delete

curl -H "Authorization: ${ACCOUNT_AUTHORIZATION_TOKEN}" \
    -d "{\"applicationKeyId\": \"${APPLICATION_KEY_ID}\"}" \
    "${API_URL}/b2api/v2/b2_delete_key"

Output

{
  "accountId": "12f634bf3cbz",
  "applicationKeyId": "00512f95cf4dcf0000000004z",
  "bucketId": "e1256f0973908bfc71ed0c1z",
  "capabilities": [
    "listFiles",
    "readFiles"
  ],
  "expirationTimestamp": 1671148244882,
  "keyName": "key-0003",
  "namePrefix": "foo",
  "options": [
    "s3"
  ]
}

Code

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class DeleteKey {
    public static void main(String[] args) {
        String apiUrl = ""; // Provided by b2_authorize_account
        String accountAuthorizationToken = ""; // Provided by b2_authorize_account
        String applicationKeyId = "";
        HttpURLConnection connection = null;

        String postParams = "{\"applicationKeyId\": \"" + applicationKeyId + "\"}";
        byte[] postData = postParams.getBytes(StandardCharsets.UTF_8);

        try {
            URL url = new URL(apiUrl + "/b2api/v2/b2_delete_key");
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", accountAuthorizationToken);
            connection.setRequestProperty("Content-Type", "application/json");
            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 jsonResponse = myInputStreamReader(connection.getInputStream());
            System.out.println(jsonResponse);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                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

{
  "accountId": "12f634bf3cbz",
  "applicationKeyId": "00512f95cf4dcf0000000004z",
  "bucketId": "e1256f0973908bfc71ed0c1z",
  "capabilities": [
    "listFiles",
    "readFiles"
  ],
  "expirationTimestamp": 1671148244882,
  "keyName": "key-0003",
  "namePrefix": "foo",
  "options": [
    "s3"
  ]
}

Code

import requests

api_url = ""  # Provided by b2_authorize_account
account_authorization_token = ""  # Provided by b2_authorize_account
application_key_id = "" # The key to delete

response = requests.post(f'{api_url}/b2api/v2/b2_delete_key',
                         json={'applicationKeyId': application_key_id},
                         headers={'Authorization': account_authorization_token})
print(response.text)

Output

{
  "accountId": "12f634bf3cbz",
  "applicationKeyId": "00512f95cf4dcf0000000004z",
  "bucketId": "e1256f0973908bfc71ed0c1z",
  "capabilities": [
    "listFiles",
    "readFiles"
  ],
  "expirationTimestamp": 1671148244882,
  "keyName": "key-0003",
  "namePrefix": "foo",
  "options": [
    "s3"
  ]
}

Code

import Foundation

let apiUrl = "" // Provided by b2_authorize_account
let accountAuthorizationToken = "" // Provided by b2_authorize_account
let applicationKeyId = "" // The key to delete

// Create the request
var request = URLRequest(url: URL(string: "\(apiUrl)/b2api/v2/b2_delete_key")!)
request.httpMethod = "POST"
request.addValue(accountAuthorizationToken, forHTTPHeaderField: "Authorization")
request.httpBody = "{\"applicationKeyId\": \"\(applicationKeyId)\"}".data(using: .utf8)!

// Create the task
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
	if let data = data {
		let json = String(data: data, encoding: .utf8)
		print("\(json!)")
	}
}

// Start the task
task.resume()

Output

{
  "accountId": "12f634bf3cbz",
  "applicationKeyId": "00512f95cf4dcf0000000004z",
  "bucketId": "e1256f0973908bfc71ed0c1z",
  "capabilities": [
    "listFiles",
    "readFiles"
  ],
  "expirationTimestamp": 1671148244882,
  "keyName": "key-0003",
  "namePrefix": "foo",
  "options": [
    "s3"
  ]
}

Code

require 'json'
require 'net/http'

api_url = "" # Provided by b2_authorize_account
account_authorization_token = "" # Provided by b2_authorize_account
application_key_id = "" # The key to delete

uri = URI("#{api_url}/b2api/v2/b2_delete_key")
req = Net::HTTP::Post.new(uri)
req.add_field("Authorization","#{account_authorization_token}")
req.body = "{\"applicationKeyId\":\"#{application_key_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
    puts res.body
when Net::HTTPRedirection then
    fetch(res['location'], limit - 1)
else
    res.error!
end

Output

{
  "accountId": "12f634bf3cbz",
  "applicationKeyId": "00512f95cf4dcf0000000004z",
  "bucketId": "e1256f0973908bfc71ed0c1z",
  "capabilities": [
    "listFiles",
    "readFiles"
  ],
  "expirationTimestamp": 1671148244882,
  "keyName": "key-0003",
  "namePrefix": "foo",
  "options": [
    "s3"
  ]
}

Code

using System.Net;
using System.Net.Http;
using System.Text;

var apiUrl = ""; //Provided by b2_authorize_account 
var accountAuthorizationToken = ""; //Provided by b2_authorize_account
var applicationKeyId = ""; // The key to delete

var body = "{\"applicationKeyId\": \"" + applicationKeyId + "\"}";

HttpClient client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, 
    new Uri(apiUrl + "/b2api/v2/b2_delete_key"));
request.Headers.TryAddWithoutValidation("Authorization", accountAuthorizationToken);
request.Content = new StringContent(body, Encoding.UTF8, "application/json");

using var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();

Console.WriteLine(responseBody);

Output

{
  "accountId": "12f634bf3cbz",
  "applicationKeyId": "00512f95cf4dcf0000000004z",
  "bucketId": "e1256f0973908bfc71ed0c1z",
  "capabilities": [
    "listFiles",
    "readFiles"
  ],
  "expirationTimestamp": 1671148244882,
  "keyName": "key-0003",
  "namePrefix": "foo",
  "options": [
    "s3"
  ]
}

Code

$api_url = ""; // From b2_authorize_account call
$auth_token = ""; // From b2_authorize_account call
$application_key_id = ""; // The key to delete

$session = curl_init($api_url .  "/b2api/v2/b2_delete_key");

// Add post fields
$data = array("applicationKeyId" => $application_key_id);
$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);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($session);
curl_close ($session);

echo ($server_output);

Output

{
  "accountId": "12f634bf3cbz",
  "applicationKeyId": "00512f95cf4dcf0000000004z",
  "bucketId": "e1256f0973908bfc71ed0c1z",
  "capabilities": [
    "listFiles",
    "readFiles"
  ],
  "expirationTimestamp": 1671148244882,
  "keyName": "key-0003",
  "namePrefix": "foo",
  "options": [
    "s3"
  ]
}