All Products
Search
Document Center

Object Storage Service:Delete files (Node.js SDK)

Last Updated:Jun 02, 2026

Permanently or temporarily delete single objects, multiple objects, or objects by 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 and x-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 of x-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 in params. To delete the version whose ID is null, add params['versionId'] = "null" to params. 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

    Permanently delete an object by specifying its version ID:

    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

    Temporarily delete an object without specifying a version ID:

    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

Permanently or temporarily delete multiple objects.

  • Permanent deletion

    Permanently delete multiple objects and delete markers by specifying version IDs:

    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 code shows how to temporarily delete multiple objects without specifying their versionIds. Temporarily deleted object versions can be recovered.

    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 objects by prefix

Delete objects 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