This topic describes how to delete single files, multiple files, or files with a specified prefix from a versioning-enabled bucket.
Delete operations on a versioning-enabled bucket
When you delete an object from a versioning-enabled bucket, you must determine whether to specify a version ID in the request.
Delete an object without specifying a version ID (temporary deletion)
By default, if you do not specify the version ID of the object that you want to delete in the request, OSS does not delete the current version of the object but adds a delete marker to the object as the latest version. If you perform the GetObject operation on the object, OSS identifies the current version of the object as a delete marker and returns
404 Not Found. In addition,header:x-oss-delete-marker = trueandx-oss-version-idthat indicates the version ID of the delete marker are included in the response.If the value of
x-oss-delete-markeris true, the value ofx-oss-version-idis the version ID of a delete marker.Delete an object by specifying a version ID (permanent deletion)
If you specify the version ID of the object that you want to delete in the request, OSS permanently deletes the specified version of the object based on the
versionIdparameter specified inparams. To delete the version whose ID is null, addparams['versionId'] = "null"toparams. OSS identifies the string "null" as the ID of the version to delete and deletes the version whose ID is null.
Delete a single file
The following examples show how to permanently or temporarily delete an object from a versioning-enabled bucket.
Permanent deletion
The following code shows how to permanently delete an object by specifying its versionId:
const OSS = require("ali-oss"); const client = new OSS({ // Replace yourregion with 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: 'yourregion', // Obtain access credentials from environment variables. Before running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); // Specify the versionId of the object. const versionId = "versionId"; // Specify the object. const objectName = "exampleobject.txt"; async function deleteVersionObject() { const result = await client.delete(objectName, { versionId, }); console.log(result); } deleteVersionObject();Temporary deletion
The following code shows how to temporarily delete an object without specifying a versionId:
const OSS = require('ali-oss'); const client = new OSS({ // Replace yourregion with 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: 'yourregion', // Obtain access credentials from environment variables. Before running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); // Temporarily delete the object without specifying a versionId. This operation adds a delete marker to the object. // Specify the object. const objectName = "exampleobject.txt"; async function deleteObject() { const result = await client.delete(objectName); console.log(result); } deleteObject();
Delete multiple files
The following examples show how to permanently and temporarily delete multiple objects.
Permanently delete
The following code provides an example of permanently deleting multiple objects and their delete markers by specifying their versionIds:
const OSS = require('ali-oss'); const client = new OSS({ // Replace yourregion with 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: 'yourregion', // Obtain access credentials from environment variables. Before running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); // Delete multiple objects or delete markers by specifying their versionIds. const names = [ { key: 'key1.js', versionId: 'versionId1' }, { key: 'key2.js', versionId: 'versionId2' } ]; async function deleteMulti() { const result = await client.deleteMulti(names); console.log(result); } deleteMulti();Temporary deletion
The following sample code provides an example on how to temporarily delete multiple objects from a versioning-enabled bucket without specifying version IDs. You can restore the current versions of the objects after they are temporarily deleted.
const OSS = require('ali-oss'); const client = new OSS({ // Replace yourregion with 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: 'yourregion', // Obtain access credentials from environment variables. Before running this sample code, make sure 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, // Replace yourbucketname with the bucket name. bucket: 'yourbucketname' }); const names = ['key1.js', 'key2.js']; async function deleteMulti() { // Delete multiple objects without specifying versionIds. const result = await client.deleteMulti(names); console.log(result); } deleteMulti();
Delete files with a specified prefix
The following code shows how to delete files with a specified prefix:
const OSS = require("ali-oss");
const client = new OSS({
// Replace yourregion with 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: 'yourregion',
// Obtain access credentials from environment variables. Before running this sample code, make sure 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,
// Replace yourbucketname with the bucket name.
bucket: 'yourbucketname'
});
// Specify the prefix of the objects to delete.
const prefix = "test";
async function deleteMutiPrefix() {
// Get the versionId information for objects with the specified prefix.
const list = await client.getBucketVersions({
prefix: prefix,
});
for (let i = 0; i < list.objects.length; i++) {
const obj = list.objects[i];
// Delete the object with the specified prefix.
const versionId = obj.versionId;
await client.delete(obj.name, {
versionId,
});
}
}
deleteMutiPrefix();References
For more information about the DeleteObject API operation, see DeleteObject.
For more information about the DeleteMultipleObjects API operation, see DeleteMultipleObjects.