This topic describes how to delete a single object, multiple objects, or objects whose names contain 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 = true
andx-oss-version-id
that indicates the version ID of the delete marker are included in the response.If the value of
x-oss-delete-marker
is true, the value ofx-oss-version-id
is 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
versionId
parameter 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 an object at a time
The following examples show how to permanently or temporarily delete an object from a versioning-enabled bucket.
Permanently delete an object from a versioning-enabled bucket
The following sample code provides an example on how to permanently delete an object from a versioning-enabled bucket by specifying the version ID of the object in the request:
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: 'yourregion', // 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: 'yourbucketname' }); // Specify the version ID 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();
Temporarily delete an object from a versioning-enabled bucket
The following sample code provides an example on how to temporarily delete an object from a versioning-enabled bucket by sending a request in which no version ID is specified:
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: 'yourregion', // 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: 'yourbucketname' }); // Temporarily delete an object without specifying its version ID. A delete marker is added 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 objects at a time
The following examples describe how to permanently or temporarily delete multiple objects from a versioning-enabled bucket at a time.
Permanently delete multiple objects from a versioning-enabled bucket at a time
The following sample code provides an example on how to permanently delete multiple objects or delete markers from a versioning-enabled bucket by specifying the version IDs of the objects or delete markers in the request:
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: 'yourregion', // 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: 'yourbucketname' }); // Delete the objects with specified version IDs or the objects whose current versions are delete markers with specified version IDs. 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();
Temporarily delete multiple objects from a versioning-enabled bucket at a time
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({ // 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: 'yourregion', // 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: 'yourbucketname' }); const names = ['key1.js', 'key2.js']; async function deleteMulti() { // Delete multiple objects without specifying their version IDs. const result = await client.deleteMulti(names); console.log(result); } deleteMulti();
Delete objects whose names contain a specified prefix
The following sample code provides an example on how to delete objects whose names contain a specified prefix:
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: 'yourregion',
// 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: 'yourbucketname'
});
// Specify the prefix in the names of the objects that you want to delete.
const prefix = "test";
async function deleteMutiPrefix() {
// List the versions of the objects whose names contain 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 objects whose names contain the specified prefix.
const versionId = obj.versionId;
await client.delete(obj.name, {
versionId,
});
}
}
deleteMutiPrefix();
References
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.