Tüm Ürünler
Search
Belge Merkezi

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

Son Güncelleme:Oct 01, 2026

Sürüm oluşturmanın etkin olduğu bir paketten tekli nesneleri, birden çok nesneyi veya ön eke göre nesneleri kalıcı ya da geçici olarak silin.

Sürümlü paketlerde silme davranışı

Sürümlü bir paketten bir nesne sildiğinizde, bir sürüm kimliği belirtmeniz gerekip gerekmediğine karar verin.

  • Sürüm kimliği belirtmeden bir nesne silme (geçici silme)

    Bir sürüm kimliği belirtmezseniz OSS geçerli sürümü silmez. Bunun yerine, en son sürüm olarak bir silme işaretçisi ekler. Sonraki GetObject istekleri, yanıtta silme işaretçisinin header:x-oss-delete-marker = true ve x-oss-version-id değerleriyle birlikte 404 Not Found döndürür.

    x-oss-delete-marker değeri true olduğunda, x-oss-version-id silme işaretçisinin sürüm kimliğini döndürür.

  • Sürüm kimliği belirterek bir nesne silme (kalıcı silme)

    Bir sürüm kimliği belirtirseniz, OSS params içindeki versionId parametresini kullanarak ilgili sürümü kalıcı olarak siler. Kimliği null olan sürümü silmek için params içinde params['versionId'] = "null" olarak ayarlayın. OSS, "null" dizesini hedef sürüm kimliği olarak değerlendirir.

Tek bir dosyayı silme

Sürümlü bir paketten bir nesneyi kalıcı ya da geçici olarak silin.

  • Kalıcı silme

    Sürüm kimliğini belirterek bir nesneyi kalıcı olarak silin:

    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();
  • Geçici silme

    Bir sürüm kimliği belirtmeden bir nesneyi geçici olarak silin:

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

Birden çok dosyayı silme

Birden çok nesneyi kalıcı ya da geçici olarak silin.

  • Kalıcı silme

    Sürüm kimliklerini belirterek birden çok nesneyi ve silme işaretçisini kalıcı olarak silin:

    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();
  • Geçici silme

    Aşağıdaki kod, sürüm kimliklerini belirtmeden birden çok nesneyi nasıl geçici olarak sileceğinizi gösterir. Geçici olarak silinen nesne sürümleri kurtarılabilir.

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

Ön eke göre nesneleri silme

Belirtilen ön eke sahip nesneleri silin:

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

Başvurular