You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage Object
Deleting Objects (Java SDK)
Copy page
Download PDF
Deleting Objects (Java SDK)
When objects are no longer in use, you can delete them to reduce cost overhead. The TOS Java SDK supports deleting individual objects from a bucket as well as deleting multiple specified objects. For buckets with versioning enabled, you can delete data for a specified version of an object.​
Notes​
  • To delete an object, your account must have the tos:DeleteObject permission. For detailed instructions, please refer to the Access control.​
  • To delete multi-version objects, your account must have the tos:DeleteObjectVersion permission. For detailed instructions, please refer to the Access control.​
  • Please exercise caution when performing delete operations, as files cannot be restored once deleted.​
Sample code​
Delete an object​
The following code demonstrates how to delete the example_object.txt file located in the example_dir directory of the specified bucket bucket-example.​
​
import com.volcengine.tos.TOSV2;​
import com.volcengine.tos.TOSV2ClientBuilder;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.object.DeleteObjectInput;​
import com.volcengine.tos.model.object.DeleteObjectOutput;​
​
public class DeleteObjectExample {​
public static void main(String[] args) {​
String endpoint = "your endpoint";​
String region = "your region";​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
​
String bucketName = "bucket-example";​
String objectKey = "example_dir/example_object.txt";​
​
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try{​
DeleteObjectInput input = new DeleteObjectInput().setBucket(bucketName).setKey(objectKey);​
DeleteObjectOutput output = tos.deleteObject(input);​
System.out.println("deleteObject succeed.");​
} catch (TosClientException e) {​
System.out.println("deleteObject failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
System.out.println("deleteObject failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
System.out.println("deleteObject failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Delete an object version​
The following code demonstrates how to delete the example_object.txt file located in the example_dir directory of the multi-version bucket bucket-example by specifying its version number.​
​
import com.volcengine.tos.TOSV2;​
import com.volcengine.tos.TOSV2ClientBuilder;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.object.DeleteObjectInput;​
import com.volcengine.tos.model.object.DeleteObjectOutput;​
​
public class DeleteObjectWithVersionIdExample {​
public static void main(String[] args) {​
String endpoint = "your endpoint";​
String region = "your region";​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
​
String bucketName = "bucket-example";​
String objectKey = "example_dir/example_object.txt";​
String versionId = "the specific version id";​
​
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try{​
DeleteObjectInput input = new DeleteObjectInput().setBucket(bucketName).setKey(objectKey).setVersionID(versionId);​
DeleteObjectOutput output = tos.deleteObject(input);​
System.out.println("deleteObject succeed.");​
System.out.println("is the object a delete marker? " + output.isDeleteMarker());​
System.out.println("the deleted version id is " + output.getVersionID());​
} catch (TosClientException e) {​
System.out.println("deleteObject failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
System.out.println("deleteObject failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
System.out.println("deleteObject failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Batch delete objects​
You can use the Java SDK's deleteMultiObjects interface to delete multiple objects in a single request. The following code demonstrates how to delete multiple objects from the specified bucket 'bucket-example'.​
tip​
The response mode supported for batch object deletion can be configured using quiet:​
  • When quiet is set to false, the response will include the deletion result regardless of whether the object was successfully deleted.​
  • When quiet is set to true, only the results of objects that failed to be deleted are returned in the response; objects not returned are considered to have been deleted successfully.​
​
import com.volcengine.tos.TOSV2;​
import com.volcengine.tos.TOSV2ClientBuilder;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.object.*;​
​
import java.util.ArrayList;​
import java.util.List;​
​
public class DeleteMultiObjectsExample {​
public static void main(String[] args) {​
String endpoint = "your endpoint";​
String region = "your region";​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
​
String bucketName = "bucket-example";​
String objectKey = "example_dir/example_object.txt";​
String objectKey1 = "example_dir/example_object_1.txt";​
String objectKey2 = "example_dir/example_object_2.txt";​
​
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try{​
List<ObjectTobeDeleted> objs = new ArrayList<>();​
objs.add(new ObjectTobeDeleted().setKey(objectKey));​
objs.add(new ObjectTobeDeleted().setKey(objectKey1));​
objs.add(new ObjectTobeDeleted().setKey(objectKey2));​
DeleteMultiObjectsV2Input input = new DeleteMultiObjectsV2Input().setBucket(bucketName).setObjects(objs);​
DeleteMultiObjectsV2Output output = tos.deleteMultiObjects(input);​
System.out.println("deleteMultiObjects succeed.");​
if (output.getDeleteds() != null) {​
for (int i = 0; i < output.getDeleteds().size(); i++){​
Deleted deleted = output.getDeleteds().get(i);​
System.out.println("deleted object is " + deleted);​
}​
}​
if (output.getErrors() != null) {​
for (int i = 0; i < output.getErrors().size(); i++){​
DeleteError error = output.getErrors().get(i);​
System.out.println("delete error is " + error);​
}​
}​
} catch (TosClientException e) {​
System.out.println("deleteMultiObjects failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
System.out.println("deleteMultiObjects failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
System.out.println("deleteMultiObjects failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Batch delete object versions​
The following code demonstrates how to delete multiple object versions by specifying version numbers in the designated bucket bucket-example.​
​
import com.volcengine.tos.TOSV2;​
import com.volcengine.tos.TOSV2ClientBuilder;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.object.*;​
​
import java.util.ArrayList;​
import java.util.List;​
​
public class DeleteMultiObjectsWithVersionsExample {​
public static void main(String[] args) {​
String endpoint = "your endpoint";​
String region = "your region";​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
​
String bucketName = "bucket-example";​
String objectKey = "example_dir/example_object.txt";​
String versionId = "the version id of objectKey";​
String objectKey1 = "example_dir/example_object_1.txt";​
String versionId1 = "the version id of objectKey1";​
String objectKey2 = "example_dir/example_object_2.txt";​
String versionId2 = "the version id of objectKey2";​
​
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try{​
List<ObjectTobeDeleted> objs = new ArrayList<>();​
objs.add(new ObjectTobeDeleted().setKey(objectKey).setVersionID(versionId));​
objs.add(new ObjectTobeDeleted().setKey(objectKey1).setVersionID(versionId1));​
objs.add(new ObjectTobeDeleted().setKey(objectKey2).setVersionID(versionId2));​
DeleteMultiObjectsV2Input input = new DeleteMultiObjectsV2Input().setBucket(bucketName).setObjects(objs);​
DeleteMultiObjectsV2Output output = tos.deleteMultiObjects(input);​
System.out.println("deleteMultiObjects succeed.");​
if (output.getDeleteds() != null) {​
for (int i = 0; i < output.getDeleteds().size(); i++){​
Deleted deleted = output.getDeleteds().get(i);​
System.out.println("deleted object is " + deleted);​
}​
}​
if (output.getErrors() != null) {​
for (int i = 0; i < output.getErrors().size(); i++){​
DeleteError error = output.getErrors().get(i);​
System.out.println("delete error is " + error);​
}​
}​
} catch (TosClientException e) {​
System.out.println("deleteMultiObjects failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
System.out.println("deleteMultiObjects failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
System.out.println("deleteMultiObjects failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Delete objects with a specified prefix in the bucket​
The following code demonstrates how to delete objects with the example_dir/ prefix from the specified bucket bucket-example.​
danger​
In the following code, if the prefix parameter is not set (that is, if prefix is null), setting prefix = "" (an empty string) will delete all objects in the bucket. Please check carefully before use.​
​
import com.volcengine.tos.TOSV2;​
import com.volcengine.tos.TOSV2ClientBuilder;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.object.DeleteObjectInput;​
import com.volcengine.tos.model.object.ListObjectsType2Input;​
import com.volcengine.tos.model.object.ListObjectsType2Output;​
​
public class DeleteObjectWithPrefixExample {​
public static void main(String[] args) {​
String endpoint = "your endpoint";​
String region = "your region";​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
​
String bucketName = "bucket-example";​
String prefix = "example_dir/";​
​
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try{​
boolean isTruncated = true;​
String continuationToken = null;​
while (isTruncated) {​
ListObjectsType2Input input = new ListObjectsType2Input().setBucket(bucketName)​
.setPrefix(prefix).setContinuationToken(continuationToken);​
ListObjectsType2Output output = tos.listObjectsType2(input);​
if (output.getContents() != null){​
for (int i = 0; i < output.getContents().size(); i++) {​
String objectKey = output.getContents().get(i).getKey();​
DeleteObjectInput deleteInput = new DeleteObjectInput().setBucket(bucketName)​
.setKey(output.getContents().get(i).getKey());​
tos.deleteObject(deleteInput);​
System.out.println("deleteObject succeed, deleted key is " + objectKey);​
}​
}​
isTruncated = output.isTruncated();​
continuationToken = output.getNextContinuationToken();​
}​
​
} catch (TosClientException e) {​
System.out.println("deleteObject failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
System.out.println("deleteObject failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
System.out.println("deleteObject failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Related documentation​
  • For API documentation on deleting objects, see DeleteObject.​
  • For API documentation on deleting multi-version objects, please refer to DeleteMultiObjects.​
Last updated: 2026.08.03 10:11:58