tencent cloud

Cloud Object Storage

Deleting Objects

Download
Modo Foco
Tamanho da Fonte
Última atualização: 2026-05-15 15:05:13

Introduction

This document provides example code and a description for implementing the object deletion feature using the Android SDK for COS.

Must-Knows

To use the Delete Object API, you must have the permission to delete the target object. When you configure an authorization policy, set the action to cos:DeleteObject. For more information on authorization, see CAM-supported Business APIs.
To use the Delete Multiple Objects API in an anonymous access scenario, you must have the permission to delete all target objects and the batch deletion permission. When you configure an authorization policy, the action must be set to cos:DeleteObject and cos:DeleteMultipleObjects. For more information on authorization, see CAM-supported Business APIs.
To use the Delete Multiple Objects API in a non-anonymous access scenario, you must have the permission to delete all target objects. When you configure an authorization policy, set the action to cos:DeleteObject. For more information on authorization, see CAM-supported Business APIs.

Related Examples

Feature Name
Description
Example code
Delete an object.
Provides the feature to delete a single object and delete multiple objects.

Preliminary Preparation

Creating a CosXmlService

Before calling a COS API, you must first create a CosXmlService instance. For detailed code, see Creating CosXmlService.

Use Cases

Deleting an Object

Delete the specified object (DELETE Object).
String bucket = "examplebucket-1250000000"; // Bucket name. Format: BucketName-APPID
String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.

DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket,
cosPath);
cosXmlService.deleteObjectAsync(deleteObjectRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
DeleteObjectResult deleteObjectResult = (DeleteObjectResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Deleting Multiple Objects

Delete multiple objects in batches (Delete Multi Objects).
// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
List<String> objectList = new ArrayList<String>();
objectList.add("exampleobject1"); // The location identifier of the object in the bucket, i.e., the object key.
objectList.add("exampleobject2"); // The location identifier of the object in the bucket, i.e., the object key.

DeleteMultiObjectRequest deleteMultiObjectRequest =
new DeleteMultiObjectRequest(bucket, objectList);
// In Quiet mode, only information about objects that encountered errors is returned. Otherwise, the deletion result for each Object is returned.
deleteMultiObjectRequest.setQuiet(true);
cosXmlService.deleteMultiObjectAsync(deleteMultiObjectRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
DeleteMultiObjectResult deleteMultiObjectResult =
(DeleteMultiObjectResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Deleting a Directory

The concept of folders in COS is simulated by using / to separate object names, forming a file system-like path. Therefore, deleting a folder in COS is equivalent to deleting a batch of objects that share the same prefix. For example: the folder prefix/ represents all objects prefixed with prefix/. Thus, deleting prefix/ means deleting all objects prefixed with prefix/.
Currently, the COS Android SDK does not provide an API to support this operation. However, you can achieve the same effect by combining basic operations.
// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String prefix = "folder1/"; // Specify the prefix.

GetBucketRequest getBucketRequest = new GetBucketRequest(bucket);
// prefix indicates the folder to be deleted.
getBucketRequest.setPrefix(prefix);
// Set the maximum number of objects to be traversed, and the maximum number of objects supported by a listobject is 1,000.
getBucketRequest.setMaxKeys(1000);
GetBucketResult getBucketResult = null;

do {
try {
getBucketResult = cosXmlService.getBucket(getBucketRequest);
List<ListBucket.Contents> contents = getBucketResult.listBucket.contentsList;
DeleteMultiObjectRequest deleteMultiObjectRequest = new DeleteMultiObjectRequest(bucket);
for (ListBucket.Contents content : contents) {
deleteMultiObjectRequest.setObjectList(content.key);
}
cosXmlService.deleteMultiObject(deleteMultiObjectRequest);
getBucketRequest.setMarker(getBucketResult.listBucket.nextMarker);
} catch (CosXmlClientException e) {
e.printStackTrace();
return;
} catch (CosXmlServiceException e) {
e.printStackTrace();
return;
}
} while (getBucketResult.listBucket.isTruncated);

SDK API Reference

For detailed parameters and method descriptions of all SDK interfaces, see SDK API Reference.

API Operations

For the description of the API for deleting a single object, see the DELETE Object document.
For the description of the API for deleting multiple objects, see the DELETE Multiple Objects document.

Ajuda e Suporte

Esta página foi útil?

comentários