Delete bucket
Updated at:2025-11-03
Basic workflow
- Instantiate the BOSClient class.
- Call the BOSClient.deleteBucket() method.
- The deleteBucket() method does not return any value. If the deletion fails, an exception will be thrown.
Example code
Java
1client.deleteBucket(<BucketName>); //Specify bucket name
Note: A bucket cannot be deleted if it is not empty (i.e., it contains objects or unfinished multipart uploads). The bucket must be emptied before successful deletion.
Complete example
Java
1import android.app.Activity;
2import android.os.Bundle;
3import com.baidubce.BceClientException;
4import com.baidubce.BceServiceException;
5import com.baidubce.auth.DefaultBceCredentials;
6import com.baidubce.development.R;
7import com.baidubce.services.bos.BosClient;
8import com.baidubce.services.bos.BosClientConfiguration;
9
10public class ExampleActivity extends Activity {
11 private String bucketName = <BucketName>;
12 @Override
13 protected void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
16 new Thread(new Runnable() {
17 @Override
18 public void run() {
19 try {
20 BosClientConfiguration config = new BosClientConfiguration();
21 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
22 config.setEndpoint(<EndPoint>);
23 BosClient client = new BosClient(config);
24 // Delete bucket
25 client.deleteBucket(<BucketName>); //Specify bucket name
26 } catch (BceServiceException e) {
27 System.out.println("Error ErrorCode: " + e.getErrorCode());
28 System.out.println("Error RequestId: " + e.getRequestId());
29 System.out.println("Error StatusCode: " + e.getStatusCode());
30 System.out.println("Error Message: " + e.getMessage());
31 System.out.println("Error ErrorType: " + e.getErrorType());
32 } catch (BceClientException e) {
33 System.out.println("Error Message: " + e.getMessage());
34 }
35 }
36 }).start();
37}
38}
