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

Torch Object Storage

Copy page
Download PDF
Introduction to Typical Scenes
Deletion Scenarios (Java SDK)
Copy page
Download PDF
Deletion Scenarios (Java SDK)
This article introduces code examples for common object deletion scenarios.​
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 specific instructions, please refer to the Access control.​
  • Please exercise caution when performing delete operations, as files cannot be restored once deleted.​
Delete an object​
For an example of how to delete a single regular object, see Delete an object.​
Delete an object version​
For an example of how to delete a single multi-version object, refer to Delete an object version.​
Delete all objects in the specified directory​
warning​
This operation will delete all objects in the specified directory. Please use with caution.​
For an example of how to delete all objects within a specified directory, refer to Delete objects with a specified prefix in a bucket.​
Delete all multi-version objects in the specified directory​
warning​
This operation will delete all multi-version objects in the specified directory. Please proceed with caution.​
For an example of how to delete all versions of all objects with a specified prefix, refer to Delete object versions—Delete all objects with a specified prefix.​
Clear all incomplete multipart uploads for objects under the specified prefix in the bucket​
The following code demonstrates how to delete all incomplete multipart uploads for objects under a specified prefix in the bucket-example bucket.​
​
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.*;​
​
public class AbortMultipartUploadWithPrefixExample {​
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 keyMarker = null;​
String uploadIdMarker = null;​
while (isTruncated) {​
ListMultipartUploadsV2Input input = new ListMultipartUploadsV2Input().setBucket(bucketName)​
.setKeyMarker(keyMarker).setUploadIDMarker(uploadIdMarker).setPrefix(prefix);​
ListMultipartUploadsV2Output output = tos.listMultipartUploads(input);​
System.out.printf("listMultipartUploads succeed, is truncated? %b, next uploadIDMarker is %s, " +​
"next keyMarker is %s.\n", output.isTruncated(), output.getNextUploadIdMarker(), output.getNextKeyMarker());​
if (output.getUploads() != null) {​
for (int i = 0; i < output.getUploads().size(); i++) {​
ListedUpload upload = output.getUploads().get(i);​
AbortMultipartUploadInput abort = new AbortMultipartUploadInput().setBucket(bucketName)​
.setKey(upload.getKey()).setUploadID(upload.getUploadID());​
tos.abortMultipartUpload(abort);​
System.out.println("Aborted upload is " + upload);​
}​
}​
isTruncated = output.isTruncated();​
keyMarker = output.getNextKeyMarker();​
uploadIdMarker = output.getNextUploadIdMarker();​
}​
} catch (TosClientException e) {​
System.out.println("abortMultipartUpload failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
System.out.println("abortMultipartUpload 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("abortMultipartUpload failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
​
Last updated: 2026.08.03 10:22:24