You can delete a single or multiple objects that you no longer need.

Warning Deleted objects cannot be recovered. Exercise caution when you delete objects.

Usage notes

  • When you use packaging tools such as Webpack and Browserify, install OSS SDK for Browser.js by running the npm install ali-oss command.
  • OSS SDK for Browser.js is usually used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use a temporary access credential generated by Security Token Service (STS) to access OSS.

    For more information about how to set up STS, see Use a temporary access credential provided by STS to access OSS in OSS Developer Guide. You can call the AssumeRole operation or use STS SDKs for various programming languages to obtain a temporary access credential. For more information, see STS SDK overview. A temporary access credential contains a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret.

Delete a single object

The following code provides an example on how to delete a single object:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>
  <button id="delete">Delete An Object</button> 
  <!-- import an SDK file -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"></script>
  <script type="text/javascript">
    // To prevent the AccessKey pair of your Alibaba Cloud account from being leaked, we recommend that you use a temporary access credential provided by Security Token Service (STS) to access OSS. For more information, go to https://help.aliyun.com/document_detail/100624.html.
    const client = new OSS({
      // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
      region: 'yourRegion',
      // Specify the temporary AccessKey pair obtained from STS. 
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // Specify the security token obtained from STS. 
      stsToken: 'yourSecurityToken',
      // Specify the name of the bucket. Example: examplebucket. 
      bucket: "examplebucket",
    });

    const deleteSingle = document.getElementById("delete");   

    // Delete a single object. 
    deleteSingle.addEventListener("click", async () => {
      // Specify the name of the object to delete. // Specify the full path of the object. The path of the object cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
      let result = await client.delete('exampledir/exampleobject.txt');
      console.log(result);
    });

  </script>
</body>

</html>

Deletes multiple objects

The following code provides an example on how to delete multiple objects:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>  
  <button id="deleteAll">Delete Objects</button>
  <!-- import an SDK file -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"></script>
  <script type="text/javascript">
    // To prevent the AccessKey pair of your Alibaba Cloud account from being leaked, we recommend that you use a temporary access credential provided by Security Token Service (STS) to access OSS. For more information, go to https://help.aliyun.com/document_detail/100624.html.
    const client = new OSS({
      // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
      region: 'yourRegion',
      // Specify the temporary AccessKey pair obtained from STS. 
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // Specify the security token obtained from STS. 
      stsToken: 'yourSecurityToken',
      // Specify the name of the bucket. Example: examplebucket. 
      bucket: "examplebucket",
    });
   
    const deleteAll = document.getElementById('deleteAll')

    // Delete multiple objects. 
    deleteAll.addEventListener('click', async () => {
      // Specify the names of the objects to delete. Specify the full path of the objects. The object names cannot contain the bucket name. 
      let result = await client.deleteMulti(['object1.txt', 'object2.txt', 'object3.txt']);
      console.log(result);

      let result = await client.deleteMulti(['object1.txt', 'object2.txt', 'object3.txt'], {
        // Set the quiet parameter to specify whether to list all objects that are deleted. If you set quiet to true, OSS does not return the list of the objects that are deleted. If you set quiet to false, OSS returns the list of the objects that are deleted. 
        quiet: true
      });
    })

  </script>
</body>

</html>

References

  • Delete a single object
    • For more information about the complete sample code that is used to delete an object, visit GitHub.
    • For more information about the API operation that is called to delete an object, see DeleteObject.
  • Delete multiple objects
    • For more information about the complete sample code that is used to delete multiple objects, visit GitHub.
    • For more information about the API operation that is called to delete multiple objects, see DeleteMultipleObjects.