Delete Object
Updated at:2025-11-03
Basic workflow
- Instantiate the BOSClient class.
- Invoke the BOSClient.deleteObject() method.
- If the operation fails, an exception is thrown; if successful, no return value is provided.
Example code
Java
1// Delete Object
2 client.deleteObject(<BucketName>, <ObjectKey>); //Specify the name of the bucket where the object to be deleted is located and the name of the object
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.demo.R;
7import com.baidubce.services.bos.BosClient;
8import com.baidubce.services.bos.BosClientConfiguration;
9public class ExampleActivity extends Activity {
10private String bucketName = <BucketName>;
11private String objectKey = <ObjectKey>;
12@Override
13protected 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 Object
25 client.deleteObject(<BucketName>, <ObjectKey>); //Specify the name of the bucket where the object to be deleted is located and the name of the object
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}}
