Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Delete objects (OSS SDK for C# 1.0)

Son Güncelleme:Oct 01, 2026

Bu konu, sürüm oluşturulmuş bir paketten tek bir nesnenin, birden çok nesnenin ve adları belirli bir ön ek içeren nesnelerin nasıl silineceğini açıklar.

Notlar

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek istiyorsanız dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı gösterir. Özel bir alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma (C# SDK V1).

  • Bir nesneyi silmek için oss:DeleteObject iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.

Sürüm oluşturulmuş paketlerde silme davranışı

Sürüm oluşturulmuş bir paketten bir nesne sildiğinizde, bir sürüm kimliği belirtip belirtmeyeceğinize karar vermeniz gerekir.

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

    Sürüm kimliği belirtmezseniz OSS mevcut sürümü silmez. Bunun yerine en son sürüm olarak bir silme belirteci ekler. Sonraki GetObject istekleri, yanıtta 404 Not Found durumunu, header:x-oss-delete-marker = true başlığını ve silme belirtecinin x-oss-version-id değerini döndürür.

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

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

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

Tek bir nesne silme

Aşağıdaki örnekler, sürüm oluşturulmuş bir paketten tek bir nesnenin kalıcı veya geçici olarak nasıl silineceğini gösterir.

  • Kalıcı silme

    Aşağıdaki örnek kod, istekte nesnenin sürüm kimliğini belirterek sürüm oluşturulmuş bir paketten bir nesne sürümünün kalıcı olarak nasıl silineceğini gösterir:

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    var endpoint = "yourEndpoint";
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
    var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
    // Specify the name of the bucket. Example: examplebucket. 
    var bucketName = "examplebucket";
    // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    var objectName = "exampledir/exampleobject.txt";
    // Specify the version ID of the object or the delete marker. 
    var versionid = "yourObjectVersionidOrDelMarkerVersionid";
    // 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 cn-hangzhou.
    const string region = "cn-hangzhou";
    
    // Create a ClientConfiguration instance and modify the default parameters based on your requirements.
    var conf = new ClientConfiguration();
    
    // Use the signature algorithm V4.
    conf.SignatureVersion = SignatureVersion.V4;
    
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
    client.SetRegion(region);
    try
    {
        // Specify the version ID of the object or the delete marker that you want to delete. 
        var request = new DeleteObjectRequest(bucketName, objectName)
        {
            VersionId = versionid
        };
        client.DeleteObject(request);
        Console.WriteLine("Delete object succeeded");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Delete object failed. {0}", ex.Message);
    }
  • Geçici silme

    Aşağıdaki örnek kod, sürüm kimliği belirtilmeyen bir istek göndererek sürüm oluşturulmuş bir paketten bir nesnenin geçici olarak nasıl silineceğini gösterir:

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    var endpoint = "yourEndpoint";
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
    var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
    // Specify the name of the bucket. Example: examplebucket. 
    var bucketName = "examplebucket";
    // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    var objectName = "exampledir/exampleobject.txt";
    // 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 cn-hangzhou.
    const string region = "cn-hangzhou";
    
    // Create a ClientConfiguration instance and modify the default parameters based on your requirements.
    var conf = new ClientConfiguration();
    
    // Use the signature algorithm V4.
    conf.SignatureVersion = SignatureVersion.V4;
    
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
    client.SetRegion(region);
    try
    {
        // Temporarily delete an object without specifying its version ID. A delete marker is added to the object. 
        var result = client.DeleteObject(bucketName, objectName);
        Console.WriteLine("Delete object succeeded, versionid: {0}, DeleteMarker: {1}", result.VersionId, result.DeleteMarker);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Delete object failed. {0}", ex.Message);
    }

Birden çok nesne silme

Aşağıdaki örnekler, sürüm oluşturulmuş bir paketten birden çok nesnenin kalıcı veya geçici olarak nasıl silineceğini açıklar.

  • Kalıcı silme

    Aşağıdaki örnek kod, belirtilen sürüm kimliklerine sahip birden çok nesnenin veya silme belirtecinin sürüm oluşturulmuş bir paketten kalıcı olarak nasıl silineceğini gösterir:

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    var endpoint = "yourEndpoint";
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
    var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
    // Specify the name of the bucket. Example: examplebucket. 
    var bucketName = "examplebucket";
    // 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 cn-hangzhou.
    const string region = "cn-hangzhou";
    
    // Create a ClientConfiguration instance and modify the default parameters based on your requirements.
    var conf = new ClientConfiguration();
    
    // Use the signature algorithm V4.
    conf.SignatureVersion = SignatureVersion.V4;
    
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
    client.SetRegion(region);
    try
    {
        // Delete the objects with the specified version IDs or the objects whose current versions are delete markers with the specified version IDs. 
        var obj1 = new ObjectIdentifier
        {
            Key = "yourObject1Name",
            VersionId  = "yourObject1NameVersionid"
        };
    
        var obj2 = new ObjectIdentifier
        {
            Key = "yourObject2Name",
            VersionId  = "yourObject2DelMarkerVersionid"
        };
    
        IList<ObjectIdentifier> objects = new List<ObjectIdentifier>();
        objects.Add(obj1);
        objects.Add(obj2);
    
        var request = new DeleteObjectVersionsRequest(bucketName, objects);
    
        // Initiate a deleteVersions request. 
        client.DeleteObjectVersions(request);
        Console.WriteLine("DeleteObjectVersions succeeded ");
    }
    catch (OssException ex)
    {
        Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
            ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed with error info: {0}", ex.Message);
    }
  • Geçici silme

    Aşağıdaki kod, sürüm kimlikleri belirtilmeden birden çok nesnenin geçici olarak nasıl silineceğini gösterir. Geçici olarak silinen nesne sürümleri kurtarılabilir.

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    var endpoint = "yourEndpoint";
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
    var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
    // Specify the name of the bucket. Example: examplebucket. 
    var bucketName = "examplebucket";
    // 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 cn-hangzhou.
    const string region = "cn-hangzhou";
    
    // Create a ClientConfiguration instance and modify the default parameters based on your requirements.
    var conf = new ClientConfiguration();
    
    // Use the signature algorithm V4.
    conf.SignatureVersion = SignatureVersion.V4;
    
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
    client.SetRegion(region);
    try
    {
        var keys = new List<string>();
        var listResult = client.ListObjects(bucketName);
        foreach (var summary in listResult.ObjectSummaries)
        {
            keys.Add(summary.Key);
        }
        // Specify the value of quietMode. true indicates quiet. In this mode, the deleted objects are not returned. false indicates verbose. In this mode, the deleted objects are returned. Default value: false. 
        var quietMode = false;
        // Specify the return mode by setting the value of quietMode in the DeleteObjectsRequest request. 
        var request = new DeleteObjectsRequest(bucketName, keys, quietMode);
        // Delete the objects without specifying their version IDs. Delete markers are added to the objects. 
        var result = client.DeleteObjects(request);
        if ((!quietMode) && (result.Keys != null))
        {
            foreach (var obj in result.Keys)
            {
                Console.WriteLine("Delete successfully : {0} ", obj.Key);
            }
        }
        Console.WriteLine("Delete objects succeeded");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Delete objects failed. {0}", ex.Message);
    }

Adları belirli bir ön ek içeren nesneleri silme

Aşağıdaki kod, belirtilen ön eke sahip dosyaların nasıl silineceğini gösterir.

using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket. 
var bucketName = "examplebucket";
var prefix = "yourkeyPrefix";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";

// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();

// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{   
    ObjectVersionList result = null;
    var request = new ListObjectVersionsRequest(bucketName)
    {
        // Specify the prefix that is contained in the names of the objects that you want to list. 
        Prefix = prefix;
    };

    // List the versions of all objects whose names contain the specific prefix and delete the versions. 
    do {        
        result = client.ListObjectVersions(request);
        Console.WriteLine("ListObjectVersions succeeded");
        foreach (var deleteversion in result.DeleteMarkerSummaries)
        {
            var request = new DeleteObjectRequest(bucketName, deleteversion.Key)
            {
                VersionId = deleteversion.VersionId
            };
            client.DeleteObject(request);
        }

        foreach (var objectversion in result.ObjectVersionSummaries)
        {
            var request = new DeleteObjectRequest(bucketName, objectversion.Key)
            {
                VersionId = objectversion.VersionId
            };
            client.DeleteObject(request);            
        }
        request.KeyMarker = result.NextKeyMarker;
        request.NextVersionIdMarker = result.NextVersionIdMarker ;
    } while (result.IsTruncated)
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

İlgili kaynaklar

  • Bir nesneyi silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteObject.

  • Birden çok nesneyi silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteMultipleObjects.