Feature Overview
This document provides an overview of APIs and SDK code samples for deleting an object.
API | Operation | Description |
Deleting One Object | Deletes a specified object from a bucket | |
Deleting Multiple Objects | Deletes multiple objects from a bucket in a single request |
Deleting One Object
Note
This API (DELETE Object) is used to delete an object from a COS bucket. To call this API, you need to have permission to write the bucket.
Use Cases
cos.deleteObject({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: '1.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), required field */}, function(err, data) {console.log(err || data);});
Description
Parameter | ParameterDescription | Local Disk Types | Required |
Bucket | Bucket name in the format of BucketName-APPID. | String | Required |
Region | String | Required | |
Key | Object key (object name), the unique identifier of an object in a bucket. For more information, see Object Overview. | String | Required |
VersionId | Version ID of the object or delete marker to be deleted | String | Not required |
Callback function description
function(err, data) { ... }
Parameter | ParameterDescription | Local Disk Types |
err | Error code, which is returned when an error (network error or service error) occurs. If the request is successful, this parameter is empty. For more information, see Error Codes. | Object |
- statusCode | HTTP status code, such as 200, 403, and 404 | Number |
- headers | Returned headers | Object |
data | Object returned when the request is successful. If the request fails, this parameter is empty. | Object |
- statusCode | HTTP status code returned by the request, such as 200, 204, 403, and 404. If the deletion is successful or the object does not exist, an HTTP 204 or 200 status code will be returned. If the specified bucket is not found, an HTTP 404 status code will be returned. | Number |
- headers | Returned headers | Object |
Deleting Multiple Objects
Note
This API is used to delete multiple objects from a bucket in a single request, which allows a maximum number of 1,000 objects. There are two response modes for you to choose from:
Verbose and Quiet. The Verbose mode returns information on the deletion of each object, whereas the Quiet mode only returns information on the objects for which errors were reported.Use Cases
Delete multiple files:
cos.deleteMultipleObject({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Objects: [{ Key: '1.jpg' }, /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), required field */{ Key: '2.txt' }, /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), required field */]}, function(err, data) {console.log(err || data);});
Deleting multiple objects with a specified prefix (deleting files in the specified
a directory)var deleteFiles = function (marker) {cos.getBucket({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Prefix: 'a/',Marker: marker,MaxKeys: 1000,}, function (listError, listResult) {if (listError) return console.log('list error:', listError);var nextMarker = listResult.NextMarker;var objects = listResult.Contents.map(function (item) {return {Key: item.Key}});cos.deleteMultipleObject({Bucket: bucket,Region: region,Objects: objects,}, function (delError, deleteResult) {if (delError) {console.log('delete error', delError);console.log('delete stop');} else {console.log('delete result', deleteResult);if (listResult.IsTruncated === 'true') deleteFiles(nextMarker);else console.log('delete complete');}});});}deleteFiles();
Description
Parameter | ParameterDescription | Local Disk Types | Required |
Bucket | Bucket name in the format of BucketName-APPID. | String | Required |
Region | String | Required | |
Quiet | A Boolean value which determines whether to use the Quiet mode. If the value is true, the Quiet mode will be used; if it is false, the Verbose mode will be used. Default value: false | Boolean | Not required |
Objects | Lists files to be deleted | ObjectArray | Required |
- Key | Object key (object name), the unique identifier of an object in a bucket. For more information, see Object Overview. | String | Required |
- VersionId | Version ID of the object or delete marker to be deleted | String | Not required |
Callback function description
function(err, data) { ... }
Parameter | ParameterDescription | Local Disk Types |
err | Error code, which is returned when an error (network error or service error) occurs. If the request is successful, this parameter is empty. For more information, see Error Codes. | Object |
- statusCode | HTTP status code returned by the request, such as 200, 204, 403, and 404 | Number |
- headers | Returned headers | Object |
data | Object returned when the request is successful. If the request fails, this parameter is empty. | Object |
- statusCode | HTTP status code returned by the request, such as 200, 204, 403, and 404 | Number |
- headers | Returned headers | Object |
- Deleted | Lists objects deleted successfully | ObjectArray |
- - Key | Object key (object name), the unique identifier of an object in a bucket. For more information, see Object Overview. | String |
- - VersionId | If the VersionId parameter is passed in, it will also be included in the response, indicating the version of the object or delete marker. | String |
- - DeleteMarker | If versioning is enabled and the VersionId parameter is not specified, the deletion will not actually delete the file; instead, it will only add a new delete marker, indicating that the visible file has been deleted. Enumerated values: true, false | String |
- - DeleteMarkerVersionId | Returns the VersionId of the newly added delete marker if DeleteMarker is true | String |
- Error | Lists objects whose deletion failed | ObjectArray |
- - Key | Object key (object name), the unique identifier of an object in a bucket. For more information, see Object Overview. | String |
- - Code | Error code of the deletion failure | String |
- - Message | Error messages of the deletion failure | String |