Delete files (Node.js SDK)
Delete a single object, multiple objects by name, objects matching a prefix, or an entire directory.
Deleted objects cannot be recovered. Proceed with caution.
Delete a single file
The following code deletes an object named exampleobject.txt from a bucket named examplebucket:
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
});
async function deleteObject() {
try {
// Specify the full path of the object. The full path cannot contain the bucket name.
const result = await client.delete('exampleobject.txt');
console.log(result);
} catch (error) {
console.log(error);
}
}
deleteObject();
Batch delete files
You can delete up to 1,000 objects at a time by name, by prefix, or by directory.
The response for a batch delete operation can be in one of two modes:
-
Verbose mode: The response body from OSS contains the result for each deleted object. This is the default mode.
-
Quiet mode: OSS does not return a response body.
You can also configure lifecycle rules to automatically delete objects. For more information, see Lifecycle rules based on last modification time.
Delete multiple objects with specified names
The following code deletes multiple objects by name:
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
});
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 multiple objects with the specified object name prefix or in the specified directory
The following code deletes objects by prefix or deletes an entire directory:
If the prefix is not specified or is set to NULL, all objects in the bucket are deleted. Specify prefixes carefully.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket',
});
// Handle failed requests to prevent promise.all from breaking, and return the failure reason and the failed file name.
async function handleDel(name) {
try {
await client.delete(name);
} catch (error) {
error.failObjectName = name;
return error;
}
}
// Delete multiple files.
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/');
References
-
For complete sample code that shows how to delete one or more files, see the GitHub example.
-
For the API reference for deleting a single file, see DeleteObject.
-
For the API reference for deleting multiple files, see DeleteMultipleObjects.