This topic describes how to delete single or multiple objects using OSS SDK for Harmony.
Usage notes
For information about supported regions and endpoints, see Regions and endpoints.
To delete an object, you must have the
oss:DeleteObjectpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
Delete a single object
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Specify the AccessKey ID obtained from STS.
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret obtained from STS.
accessKeySecret: 'yourAccessKeySecret',
// Specify the STS token obtained from 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',
});
// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object that you want to delete.
const key = 'yourObjectName';
/**
* Delete the specified object.
* Use the deleteObject method.
*/
const deleteObject = async () => {
try {
// Use the deleteObject method to delete the object from the bucket.
const res = await client.deleteObject({
bucket, // Specify the name of the bucket.
key, // Specify the name of the object.
});
// Display the result of the deletion.
console.log(JSON.stringify(res));
} catch (err) {
// Capture exceptions.
if (err instanceof RequestError) {
// If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
console.log('code: ', err.code); // The error code.
console.log('message: ', err.message); // The error message.
console.log('requestId: ', err.requestId); // The request ID.
console.log('status: ', err.status); // The HTTP status code.
console.log('ec: ', err.ec); // The EC.
} else {
// Display other unknown types of errors.
console.log('unknown error: ', err);
}
}
};
// Call the deleteObject function to delete the object.
deleteObject();
Delete multiple objects
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Specify the AccessKey ID obtained from STS.
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret obtained from STS.
accessKeySecret: 'yourAccessKeySecret',
// Specify the STS token obtained from 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',
});
// 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.
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 deletion.
console.log(JSON.stringify(res));
} catch (err) {
// Capture exceptions.
if (err instanceof RequestError) {
// If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
console.log('code: ', err.code); // The error code.
console.log('message: ', err.message); // The error message.
console.log('requestId: ', err.requestId); // The request ID.
console.log('status: ', err.status); // The HTTP status code.
console.log('ec: ', err.ec); // The EC.
} else {
// Display other unknown types of errors.
console.log('unknown error: ', err);
}
}
};
// Call the deleteMultipleObjects function to perform the deletion.
deleteMultipleObjects();