Todos os produtos
Search
Central de documentação

Object Storage Service:Excluir arquivos (Browser.js SDK)

Última atualização: Jul 03, 2026

É possível excluir um ou mais arquivos (objetos) que não são mais necessários.

Aviso

Objetos excluídos não podem ser recuperados. Proceda com cautela.

Observações de uso

  • Ao utilizar ferramentas de empacotamento como Webpack e Browserify, instale o OSS SDK for Browser.js executando o comando npm install ali-oss.

  • Caso você tente acessar um bucket do OSS pelo navegador sem que regras de CORS estejam configuradas, a solicitação será rejeitada. Por isso, é obrigatório configurar regras de CORS no bucket para permitir o acesso via navegador. Para mais informações, consulte Instalação.

  • Geralmente, o OSS SDK for Browser.js é utilizado em navegadores. Para evitar a exposição do seu par de AccessKey, recomendamos o uso de credenciais de acesso temporárias obtidas por meio do Security Token Service (STS) para acessar o OSS.

    As credenciais de acesso temporárias incluem um par de AccessKey e um token de segurança. O par de AccessKey é composto por um AccessKey ID e um AccessKey secret. Para saber como obter essas credenciais temporárias, consulte Usar STS para autorização de acesso temporário.

Excluir um único arquivo

O código abaixo exclui um único arquivo:

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

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

<body>
  <button id="delete">Delete</button> 
  <!--Import the SDK file.-->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
  <script type="text/javascript">
    const client = new OSS({
      // Set region to 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',
      authorizationV4: true,
      // The temporary AccessKey pair (AccessKey ID and AccessKey secret) obtained from STS.
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // The security token (SecurityToken) obtained from STS.
      stsToken: 'yourSecurityToken',
      // Specify the bucket name. For example, examplebucket.
      bucket: "examplebucket",
    });

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

    // Delete a single file.
    deleteSingle.addEventListener("click", async () => {
      // Specify the name of the object to delete. The object name must be the full path of the object, not including the bucket name. For example, exampledir/exampleobject.txt.
      let result = await client.delete('exampledir/exampleobject.txt');
      console.log(result);
    });

  </script>
</body>

</html>

Excluir vários arquivos

O exemplo a seguir demonstra como excluir múltiplos arquivos:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
  </head>

  <body>
    <button id="deleteAll">Delete multiple files</button>
    <!--Import the SDK file.-->
    <script
      type="text/javascript"
      src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"
    ></script>
    <script type="text/javascript">
      const client = new OSS({
        // Set region to 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',
        authorizationV4: true,
        // The temporary AccessKey pair (AccessKey ID and AccessKey secret) obtained from STS.
        accessKeyId: 'yourAccessKeyId',
        accessKeySecret: 'yourAccessKeySecret',
        // The security token (SecurityToken) obtained from STS.
        stsToken: 'yourSecurityToken',
        // Specify the bucket name. For example, examplebucket.
        bucket: "examplebucket",
      });

      const deleteAll = document.getElementById("deleteAll");

      // Delete multiple files.
      deleteAll.addEventListener("click", async () => {
        // Specify the names of the objects to delete. The object names must be the full paths of the objects, not including the bucket name.
        let result = await client.deleteMulti([
          "example.txt",
          "exampleobject.txt",
          "newexampleobject.txt",
        ]);
        console.log(result);

        result = await client.deleteMulti(
          ["example.txt", "exampleobject.txt", "newexampleobject.txt"],
          {
            // Use the quiet parameter to specify whether to return a list of all deleted files. If quiet is set to true, OSS does not return a message body. If quiet is set to false, OSS returns a list of all deleted files.
            quiet: true,
          }
        );
      });
    </script>
  </body>
</html>

Referências