Delete file
Updated at:2025-11-03
For the sample code, please refer to File Deletion Demo
Delete single file
The following code can be referenced to delete an object:
Java
1public void deleteObject(BosClient client, String bucketName, String objectKey) {
2 // Delete Object
3 client.deleteObject(<BucketName>, <ObjectKey>); //Specify the name of the bucket where the object to be deleted is located and the name of the object
4}
Delete multiple files
You can refer to the following two methods to delete multiple objects:
Java
1// 1. In the form of a Json string
2String jsonObjectKeys = "{\"objects\": ["+"{\"key\": \"token1.h\"},"+"{\"key\": \"token2.h\"}"+"]}";
3DeleteMultipleObjectsRequest request = new DeleteMultipleObjectsRequest();
4request.setBucketName("yourBucketName");
5request.setJsonDeleteObjects(jsonObjectKeys);
6client.deleteMultipleObjects(request);
Java
1// 2. Users only need to specify the parameters
2List<String> objectKeys = new ArrayList<String>();
3objectKeys.add("object1");
4objectKeys.add("object2");
5DeleteMultipleObjectsRequest request = new DeleteMultipleObjectsRequest();
6request.setBucketName("yourBucketName");
7request.setObjectKeys(objectKeys);
8DeleteMultipleObjectsResponse response = client.deleteMultipleObjects(request);
1,000 objects can be deleted at most via a single request. The message body (body) does not exceed 2 M. The returned message body only includes erroneous object results in the deletion process; no message body is returned if all objects are deleted successfully.
