Todos os produtos
Search
Central de documentação

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

Última atualização: Sep 22, 2026

Exclua permanentemente ou temporariamente objetos individuais, vários objetos ou objetos por prefixo de um bucket com versionamento ativado.

Comportamento de exclusão em buckets com versionamento

Ao excluir um objeto de um bucket com versionamento, você deve decidir se especifica um ID de versão.

  • Excluir um objeto sem especificar um ID de versão (exclusão temporária)

    Se você não especificar um ID de versão, o OSS não exclui a versão atual. Em vez disso, adiciona um marcador de exclusão como a versão mais recente. As solicitações GetObject subsequentes retornam 404 Not Found com header:x-oss-delete-marker = true e o x-oss-version-id do marcador de exclusão na resposta.

    Quando x-oss-delete-marker é true, x-oss-version-id retorna o ID de versão do marcador de exclusão.

  • Excluir um objeto especificando um ID de versão (exclusão permanente)

    Se você especificar um ID de versão, o OSS exclui permanentemente essa versão usando o parâmetro versionId em params. Para excluir a versão com ID nulo, defina params['versionId'] = "null" em params. O OSS trata a string "null" como o ID de versão de destino.

Excluir um único arquivo

Exclua permanentemente ou temporariamente um objeto de um bucket com versionamento.

  • Exclusão permanente

    Exclua permanentemente um objeto especificando seu ID de versão:

    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();
  • Exclusão temporária

    Exclua temporariamente um objeto sem especificar um ID de versão:

    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();

Excluir vários arquivos

Exclua permanentemente ou temporariamente vários objetos.

  • Exclusão permanente

    Exclua permanentemente vários objetos e marcadores de exclusão especificando seus IDs de versão:

    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();
  • Exclusão temporária

    O código a seguir demonstra como excluir temporariamente vários objetos sem especificar seus versionIds. É possível recuperar as versões dos objetos excluídos temporariamente.

    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();

Excluir objetos por prefixo

Exclua objetos com um prefixo especificado:

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();

Referências