Delete data
Updated at:2025-11-03
Overview
Deleting data refers to removing files (objects) from a storage space (bucket). BOS allows you to perform the following deletion operations:
- Single deletion: Remove a specific object.
- Batch deletion: A maximum of 1,000 objects can be deleted in a single request.
- Automatic deletion: If you need to delete a large number of objects with certain patterns (e.g., regularly deleting objects older than a specific number of days, or emptying an entire bucket), it is recommended to use Lifecycle Management to automatically delete objects. After setting lifecycle rules, BOS will automatically delete expired objects according to the rules, which greatly reduces the number of deletion requests you need to send and improves deletion efficiency.
Operation types
BOS supports deletion via both API and SDK. The specific methods are as follows:
- Delete a single object using the DeleteObject API
- Batch delete objects using the DeleteMultipleObjects API
-
Delete an object by SDK:
- Java SDK
- [Python SDK](BOS/SDK/Python-SDK/File management/Delete file.md)
- PHP SDK
- C# SDK
- Android SDK
Example
The following is a code example for deleting objects using the Java SDK:
Delete a single object:
Plain Text
1public void deleteObject(BosClient client, String bucketName, String objectKey) {
2 // Delete Object
3 client.deleteObject(bucketName, objectKey);
4}
For batch deletion of objects, you can refer to the following two methods to delete multiple objects:
Plain Text
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);
7 // 2. Users only need to specify the parameters
8List<String> objectKeys = new ArrayList<String>();
9objectKeys.add("object1");
10objectKeys.add("object2");
11DeleteMultipleObjectsRequest request = new DeleteMultipleObjectsRequest();
12request.setBucketName("yourBucketName");
13request.setObjectKeys(objectKeys);
14DeleteMultipleObjectsResponse response = client.deleteMultipleObjects(request);
Note:
- Up to 1,000 objects can be deleted in a single request. The message body should not exceed 2 MB. If all objects are successfully deleted, no message body is returned; only error results for failed deletions are included in the returned message body.
