Delete multiple objects with version IDs specified
The following sample code permanently deletes multiple objects from a versioned bucket by specifying their version IDs:
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Specify the AccessKey ID provided by Security Token Service (STS).
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret provided by STS.
accessKeySecret: 'yourAccessKeySecret',
// Specify the STS token provided by STS.
securityToken: 'yourSecurityToken',
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
// Delete multiple objects with the specified version IDs.
const deleteMultipleObjectsByVersionId = async () => {
try {
// Call the deleteMultipleObjects method to delete multiple objects with version IDs specified.
const res = await client.deleteMultipleObjects({
bucket, // The bucket name.
delete: {
object: [
{ key: 'yourObjectName1', versionId: 'versionId1' }, // The first object and version ID.
{ key: 'yourObjectName2', versionId: 'versionId2' }, // The second object and version ID.
{ key: 'yourObjectName3', versionId: 'versionId3' } // The third object and version ID.
]
}
});
// Display the returned result.
console.log(JSON.stringify(res));
} catch (err) {
// Capture and handle request errors.
if (err instanceof RequestError) {
console.log('High-level error code: ', err.code); // Display the high-level error code. For example, NoSuchKey indicates that the object does not exist.
console.log('Error message: ', err.message); // Display the error message to provide more details.
console.log('Request ID: ', err.requestId); // Display the request ID to facilitate troubleshooting.
console.log('HTTP status code: ', err.status); // Display the HTTP status code. For example, 404 indicates the resource was not found.
console.log('Fine-grained error code: ', err.ec); // Display the fine-grained error code.
} else {
console.log('Unknown error: ', err); // Display errors other than RequestError.
}
}
};
// Call the function to perform the deletion operation.
deleteMultipleObjectsByVersionId();
Delete multiple objects without specifying the version IDs
The following sample code temporarily deletes multiple objects from a versioned bucket by sending a request in which no version IDs are specified:
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Specify the AccessKey ID of the RAM user.
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret of the RAM user.
accessKeySecret: 'yourAccessKeySecret',
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
// Specify the name of the bucket.
const bucket = 'yourBucketName';
/**
* Delete multiple objects at a time.
* Use the deleteMultipleObjects method to delete multiple objects from the bucket.
*/
const deleteMultipleObjects = async () => {
try {
// Call the deleteMultipleObjects method to delete multiple objects with version IDs specified.
const res = await client.deleteMultipleObjects({
bucket, // Specify the name of the bucket.
delete: {
// Specify the names of the objects to be deleted.
object: [
{ key: 'yourObjectName1' }, // The first object name.
{ key: 'yourObjectName2' }, // The second object name.
{ key: 'yourObjectName3' }, // The third object name.
],
},
});
// Display the result of the object deletion operation.
console.log(JSON.stringify(res));
} catch (err) {
// Capture request exceptions.
if (err instanceof RequestError) {
// For a known error, display the error codes, error message, requests ID, and HTTP status code.
console.log('High-level error code: ', err.code); // Display the high-level error code.
console.log('Error message: ', err.message); // Display the error message.
console.log('Request ID: ', err.requestId); // Display the request ID.
console.log('HTTP status code: ', err.status); // Display the HTTP status code.
console.log('Fine-grained error code: ', err.ec); // Display the fine-grained error code.
} else {
// Display other unknown types of errors.
console.log('unknown error: ', err);
}
}
};
// Call the deleteMultipleObjects function to perform the deletion operation.
deleteMultipleObjects();