All Products
Search
Document Center

Object Storage Service:Delete objects (C# SDK V2)

Last Updated:Mar 19, 2026

Use the OSS SDK for C# V2 to delete a single object or multiple objects from a bucket.

Prerequisites

Before you begin, make sure you have:

Precautions

  • The sample code uses the China (Hangzhou) region (cn-hangzhou) and the public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. See OSS regions and endpoints.

Sample code

Both examples use OSS.Configuration.LoadDefault() to initialize the client and EnvironmentVariableCredentialsProvider to load credentials from environment variables.

Delete a single object

Call DeleteObjectAsync with the bucket name and object key.

using OSS = AlibabaCloud.OSS.V2;

var region = "cn-hangzhou";          // Region where the bucket is located
var endpoint = null as string;       // Optional. Overrides the default endpoint when specified
var bucket = "<your-bucket-name>";   // Name of the bucket
var key = "<your-object-key>";       // Object key to delete

// Initialize the client
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

using var client = new OSS.Client(cfg);

// Delete the object
var result = await client.DeleteObjectAsync(new OSS.Models.DeleteObjectRequest()
{
    Bucket = bucket,
    Key = key,
});

Console.WriteLine("DeleteObject done");
Console.WriteLine($"StatusCode: {result.StatusCode}");
Console.WriteLine($"RequestId: {result.RequestId}");
Console.WriteLine("Response Headers:");
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));
Replace <your-bucket-name> and <your-object-key> with your actual values before running the code.

For the full runnable sample, see DeleteObject/Program.cs on GitHub.

Delete multiple objects

Call DeleteMultipleObjectsAsync with a list of object keys.

using OSS = AlibabaCloud.OSS.V2;

var region = "cn-hangzhou";          // Region where the bucket is located
var endpoint = null as string;       // Optional. Overrides the default endpoint when specified
var bucket = "<your-bucket-name>";   // Name of the bucket
var keys = new List<string>
{
    "<your-object-key1>",
    "<your-object-key2>",
    "<your-object-key3>",
};

// Initialize the client
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

using var client = new OSS.Client(cfg);

// Build the list of objects to delete
var objects = new List<OSS.Models.DeleteObject>();
foreach (var item in keys ?? [])
{
    objects.Add(new OSS.Models.DeleteObject() { Key = item });
}

// Delete the objects
var result = await client.DeleteMultipleObjectsAsync(new OSS.Models.DeleteMultipleObjectsRequest()
{
    Bucket = bucket,
    Objects = objects,
});

Console.WriteLine("DeleteMultipleObjects done");
Console.WriteLine($"StatusCode: {result.StatusCode}");
Console.WriteLine($"RequestId: {result.RequestId}");
Console.WriteLine("Response Headers:");
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));
Console.WriteLine("Key to be deleted:");
keys?.ForEach(x => Console.WriteLine(x));
Replace <your-bucket-name> and each <your-object-keyN> with your actual values before running the code.

For the full runnable sample, see DeleteObject/Program.cs on GitHub.

References