OSS lets you delete a single object, multiple specified objects, objects by name prefix, or an entire directory and its contents.
Deleted objects cannot be recovered. Exercise caution when performing delete operations.
Prerequisites
Before you begin, make sure that you have:
Installed the
ali-osspackage (npm install ali-oss)Set the
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variablesAn OSS bucket with permission to delete objects from it
All examples use the following client setup:
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region where the bucket is located.
// Example: if the bucket is in China (Hangzhou), set region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Load access credentials from environment variables to avoid hardcoding secrets.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
});Delete a single object
Call client.delete(objectName) with the full path of the object. The full path must not include the bucket name.
async function deleteObject() {
try {
// Specify the full path of the object, excluding the bucket name.
const result = await client.delete('exampleobject.txt');
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteObject();Batch delete objects
Use client.deleteMulti() to delete up to 1,000 objects in a single call.
Batch delete supports two response modes:
| Mode | Behavior | When to use |
|---|---|---|
| Verbose (default) | OSS returns the deletion result for each object in the response body | When you need to verify which objects were deleted |
| Quiet | OSS returns no response body | When you only need to know whether the overall operation succeeded |
Delete multiple objects with specified names
Pass an array of object full paths to client.deleteMulti(). The full paths must not include the bucket name.
async function deleteMulti() {
try {
// Specify the full paths of the objects to delete and set the response mode to verbose. The full paths cannot contain the bucket name.
// let result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt']);
// console.log(result);
// Specify the full paths of the objects to delete and set the response mode to quiet. The full paths cannot contain the bucket name.
const result = await client.deleteMulti(['exampleobject-1', 'exampleobject-2', 'testfolder/sampleobject.txt'], {quiet: true});
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteMulti();Delete objects by prefix or delete a directory
To delete all objects that share a name prefix, list the objects first, then delete them. This is the required pattern for deleting a simulated directory in OSS, which uses a flat object store with no real directory structure.
If prefix is not specified or is set to NULL in the following sample code, all objects in the bucket are deleted. Always verify the prefix before running this operation.
// Wraps individual deletes to prevent Promise.all from short-circuiting on the first error.
// On failure, attaches the object name to the error for easier debugging.
async function handleDel(name) {
try {
await client.delete(name);
} catch (error) {
error.failObjectName = name;
return error;
}
}
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);
}
// To delete all objects that have the 'src' prefix, set prefix to 'src'. This deletes files such as 'src_file.txt', the 'src' folder, and all files within the 'src' folder.
deletePrefix('src');
// To delete only the 'src' folder and all files within it, set prefix to 'src/'.
// deletePrefix('src/');What's next
To automatically delete objects on a schedule, configure lifecycle rules based on the last modified time.
For the complete sample code, see the GitHub example.
For the underlying API reference, see DeleteObject and DeleteMultipleObjects.