You can delete one or more objects at a time, delete objects whose names contain a specified prefix, or delete a specified directory and all objects in the directory.
Deleted objects cannot be recovered. Exercise caution when you delete objects.
Delete a single object
The following sample code provides an example on how to delete the exampleobject.txt object from the examplebucket bucket:
const OSS = require('ali-oss');
const client = new OSS({
// 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',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket.
bucket: 'examplebucket',
});
async function deleteObject() {
try {
// Specify the full path of the object. Do not include the bucket name in the full path.
const result = await client.delete('exampleobject.txt');
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteObject();
Delete multiple objects at a time
You can delete up to 1,000 objects at a time. You can delete multiple specified objects, objects whose names contain a specified prefix, or a specified directory and all objects in the directory.
When you delete multiple objects, the result can be returned in one of the following modes:
verbose: The response includes a message body that contains the information about all deleted objects. By default, this mode is used.
quiet: The response does not include a message body.
You can also configure lifecycle rules to automatically delete objects. For more information, see Lifecycle rules based on the last modification time.
Delete multiple specified objects
The following sample code provides an example on how to delete multiple specified objects:
const OSS = require('ali-oss');
const client = new OSS({
// 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',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket.
bucket: 'examplebucket',
});
async function deleteMulti() {
try {
// Specify the full paths of the objects that you want to delete and set the result mode to verbose. Do not include the bucket name in the full paths.
// let result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt']);
// console.log(result);
// Specify the full paths of the objects that you want to delete and set the result mode to quiet. Do not include the bucket name in the full paths.
const result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt'], {quiet: true});
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteMulti();
Delete multiple objects with a specified object name prefix or in a specified directory
The following sample code provides an example on how to delete multiple objects with a specified name prefix and how to delete a specified directory and all objects in the directory.
If the prefix is not specified or is set to NULL in the following code, all objects in the bucket are deleted. Exercise caution when you specify a prefix in a delete operation.
const OSS = require('ali-oss');
const client = new OSS({
// 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',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket.
bucket: 'examplebucket',
});
// Handle request failures, prevent the interruption of promise.all, and return failure causes and the names of objects that fail to be deleted.
async function handleDel(name) {
try {
await client.delete(name);
} catch (error) {
error.failObjectName = name;
return error;
}
}
// Delete multiple objects at a time.
async function deletePrefix(prefix) {
const list = await client.list({
prefix: prefix,
});
list.objects = list.objects || [];
const result = await Promise.all(list.objects.map((v) => handleDel(v.name)));
console.log(result);
}
// Specify the prefix in the names of the objects that you want to delete. For example, if you want to delete all objects whose names contain the src prefix, set the prefix to src. After you set the prefix to src, all non-directory objects whose names contain the src prefix, the src directory, and all objects in the src directory are deleted.
deletePrefix('src');
// If you want to delete only the src directory and all objects in the directory, set the prefix to src/.
// deletePrefix('src/');
References
For the complete sample code that is used to delete a single object or multiple objects at a time, visit GitHub.
For more information about the API operation that you can call to delete an object, see DeleteObject.
For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.