This topic describes how to delete objects.

Warning You cannot recover deleted objects. Exercise caution when you delete objects.

Delete one object

The following code provides an example on how to delete an object:

// This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Delete the object. To delete a folder, set ObjectName to the folder name. If the folder is not empty, you must delete all objects in the folder before you can delete the folder.
ossClient.deleteObject(bucketName, objectName);

// Shut down the OSSClient instance.
ossClient.shutdown();
			

Delete multiple objects

A maximum of 1,000 objects can be deleted each time. Objects can be deleted in the following two modes:

  • verbose: returns a list of deleted objects. The default mode is verbose.
  • quiet: returns a list of objects you failed to delete.

The following table describes the parameters for DeleteObjectsRequest.

Parameter Description Configuration method
Keys Specifies the objects you want to delete. setKeys(List<String>)
quiet Specifies the return mode. True indicates quiet. False indicates verbose. Default value: false. setQuiet(boolean)
encodingType Specifies the mode for encoding returned object names. Only URL encoding is supported. setEncodingType(String)

The following table describes the parameters for DeleteObjectsResult.

Parameter Description Configuration method
deletedObjects The results of the operation. The list of deleted objects is returned when the verbose mode is used. The list of objects that fail to be deleted is returned when the quiet mode is used. List<String> getDeletedObjects()
encodingType The method for encoding the objects names listed in the deletedObjects field. When the parameter is empty, the object names are not encoded. getEncodingType()

The following code provides an example on how to delete multiple objects at a time:

// This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Delete the objects. The key is the ObjectName, which indicates the complete path of the object you want to delete from OSS, and must include the file extension of the object. For example, set ObjectName to abc/efg/123.jpg.
List<String> keys = new ArrayList<String>();
keys.add("key0");
keys.add("key1");
keys.add("key2");

DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();

// Shut down the OSSClient instance.
ossClient.shutdown();
			

For the complete code used to delete multiple objects, visit GitHub.

Delete objects whose names contain a specified prefix

The following code provides an example on how to delete objects whose names contain a specified prefix:

// This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Specify a prefix.
final String prefix = "<yourkeyPrefix>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// List all objects whose names contain the specified prefix and delete them.
String nextMarker = null;
ObjectListing objectListing = null;
do {
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName)
            .withPrefix(prefix)
            .withMarker(nextMarker);

    objectListing = ossClient.listObjects(listObjectsRequest);
    if (objectListing.getObjectSummaries().size() > 0) {
        List<String> keys = new ArrayList<String>();
        for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
            System.out.println("key name: " + s.getKey());
            keys.add(s.getKey());
        }
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(keys);
        ossClient.deleteObjects(deleteObjectsRequest);
    }

    nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());

// Shut down the OSSClient instance.
ossClient.shutdown();