Delete files (Harmony SDK)

Updated at:

This topic describes how to delete one or more objects from a versioned Object Storage Service (OSS) bucket.

Usage notes

Delete operations on a versioned bucket

The delete behavior for versioning works as follows:

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

    If you perform a delete operation without specifying a versionId, a delete marker is inserted for the current version of the object instead of that version being deleted. When you call the GetObject operation, OSS detects that the current version is a delete marker and returns a 404 Not Found error. The response also returns the header: x-oss-delete-marker = true and the version ID of the new delete marker, x-oss-version-id.

    A value of true for x-oss-delete-marker indicates that the version corresponding to the returned x-oss-version-id is a delete marker.

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

    If you perform a delete operation and specify a versionId, OSS permanently deletes the object version that corresponds to the specified versionId. To delete an object that has a null versionId, set the versionId parameter to the string "null" in the API call. This action deletes the object version that has a versionId of "null".

Sample code

Delete a single object

Delete an object with the specified version ID

The following sample code permanently deletes an object from a versioned bucket by specifying its version ID:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Specify the AccessKey ID provided by Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret provided by STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the STS token provided by STS.
  securityToken: 'yourSecurityToken',
  // 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: 'oss-cn-hangzhou',
});

// Delete an object with the specified version ID.
const deleteObjectByVersionId = async () => {
  try {
    // Call the deleteObject method to delete an object based on the specified version ID.
    const res = await client.deleteObject({
      bucket, // The bucket name.
      key, // The object name.
      versionId: 'versionId' // The ID of the object version to be deleted.
    });

    // Display the returned result.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture and handle request errors.
    if (err instanceof RequestError) {
      console.log('High-level error code: ', err.code); // Display the high-level error code. For example, NoSuchKey indicates that the object does not exist.
      console.log('Error message: ', err.message); // Display the error message to provide more details.
      console.log('Request ID: ', err.requestId); // Display the request ID to facilitate troubleshooting.
      console.log('HTTP status code: ', err.status); // Display the HTTP status code. For example, 404 indicates the resource was not found.
      console.log('Fine-grained error code: ', err.ec); // Display the fine-grained error code.
    } else {
      console.log('Unknown error: ', err); // Display errors other than RequestError.
    }
  }
};

// Call the function to perform the deletion operation.
deleteObjectByVersionId();

Delete an object without specifying a version ID

The following sample code temporarily deletes an object from a versioned bucket by sending a delete request in which no version ID is specified:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Specify the AccessKey ID of the RAM user.
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret of the RAM user.
  accessKeySecret: 'yourAccessKeySecret',
  // 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: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object that you want to delete.
const key = 'yourObjectName';

/**
 * Delete the specified object. 
 * Use the deleteObject method to delete the object from the bucket. 
 */
const deleteObject = async () => {
  try {
    // Use the deleteObject method to delete the object from the bucket.
    const res = await client.deleteObject({
      bucket, // Specify the name of the bucket.
      key,    // Specify the name of the object.
    });

    // Display the result of the object deletion operation.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture request exceptions.
    if (err instanceof RequestError) {
      // For a known error, display the error codes, error message, requests ID, and HTTP status code.
      console.log('High-level error code: ', err.code); // Display the high-level error code.
      console.log('Error message: ', err.message); // Display the error message.
      console.log('Request ID: ', err.requestId); // Display the request ID.
      console.log('HTTP status code: ', err.status); // Display the HTTP status code.
      console.log('Fine-grained error code: ', err.ec); // Display the fine-grained error code.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the deleteObject function to delete the object.
deleteObject();

Delete multiple objects

Delete multiple objects with version IDs specified

The following sample code permanently deletes multiple objects from a versioned bucket by specifying their version IDs:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Specify the AccessKey ID provided by Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret provided by STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the STS token provided by STS.
  securityToken: 'yourSecurityToken',
  // 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: 'oss-cn-hangzhou',
});

// Delete multiple objects with the specified version IDs.
const deleteMultipleObjectsByVersionId = async () => {
  try {
    // Call the deleteMultipleObjects method to delete multiple objects with version IDs specified.
    const res = await client.deleteMultipleObjects({
      bucket, // The bucket name.
      delete: {
        object: [
          { key: 'yourObjectName1', versionId: 'versionId1' }, // The first object and version ID.
          { key: 'yourObjectName2', versionId: 'versionId2' }, // The second object and version ID.
          { key: 'yourObjectName3', versionId: 'versionId3' }  // The third object and version ID.
        ]
      }
    });

    // Display the returned result.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture and handle request errors.
    if (err instanceof RequestError) {
      console.log('High-level error code: ', err.code); // Display the high-level error code. For example, NoSuchKey indicates that the object does not exist.
      console.log('Error message: ', err.message); // Display the error message to provide more details.
      console.log('Request ID: ', err.requestId); // Display the request ID to facilitate troubleshooting.
      console.log('HTTP status code: ', err.status); // Display the HTTP status code. For example, 404 indicates the resource was not found.
      console.log('Fine-grained error code: ', err.ec); // Display the fine-grained error code.
    } else {
      console.log('Unknown error: ', err); // Display errors other than RequestError.
    }
  }
};

// Call the function to perform the deletion operation.
deleteMultipleObjectsByVersionId();

Delete multiple objects without specifying the version IDs

The following sample code temporarily deletes multiple objects from a versioned bucket by sending a request in which no version IDs are specified:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Specify the AccessKey ID of the RAM user.
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret of the RAM user.
  accessKeySecret: 'yourAccessKeySecret',
  // 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: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';

/**
 * Delete multiple objects at a time. 
 * Use the deleteMultipleObjects method to delete multiple objects from the bucket. 
 */
const deleteMultipleObjects = async () => {
  try {
    // Call the deleteMultipleObjects method to delete multiple objects with version IDs specified.
    const res = await client.deleteMultipleObjects({
      bucket, // Specify the name of the bucket.
      delete: {
        // Specify the names of the objects to be deleted.
        object: [
          { key: 'yourObjectName1' }, // The first object name.
          { key: 'yourObjectName2' }, // The second object name.
          { key: 'yourObjectName3' }, // The third object name.
        ],
      },
    });

    // Display the result of the object deletion operation.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture request exceptions.
    if (err instanceof RequestError) {
      // For a known error, display the error codes, error message, requests ID, and HTTP status code.
      console.log('High-level error code: ', err.code); // Display the high-level error code.
      console.log('Error message: ', err.message); // Display the error message.
      console.log('Request ID: ', err.requestId); // Display the request ID.
      console.log('HTTP status code: ', err.status); // Display the HTTP status code.
      console.log('Fine-grained error code: ', err.ec); // Display the fine-grained error code.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the deleteMultipleObjects function to perform the deletion operation.
deleteMultipleObjects();

References

  • For more information about the API operation for deleting a single file, see DeleteObject.

  • For more information about how to delete multiple files using an API operation, see DeleteMultipleObjects.