Delete files (Node.js SDK)

Updated at:

Permanently or temporarily delete single objects, multiple objects, or objects by prefix from a versioning-enabled bucket.

Delete behavior in versioned buckets

When you delete an object from a versioned bucket, you must decide whether to specify a version ID.

  • Delete an object without specifying a version ID (temporary deletion)

    If you do not specify a version ID, OSS does not delete the current version. Instead, it adds a delete marker as the latest version. Subsequent GetObject requests return 404 Not Found with header:x-oss-delete-marker = true and x-oss-version-id of the delete marker in the response.

    When x-oss-delete-marker is true, x-oss-version-id returns the version ID of the delete marker.

  • Delete an object by specifying a version ID (permanent deletion)

    If you specify a version ID, OSS permanently deletes that version using the versionId parameter in params. To delete the version with a null ID, set params['versionId'] = "null" in params. OSS treats the string "null" as the target version ID.

Delete a single file

Permanently or temporarily delete an object from a versioned 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