This topic describes how to delete a single object or multiple objects.

Warning Deleted objects cannot be recovered. Exercise caution when you delete objects.

Delete a single object

The following code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:

using Aliyun.OSS;
// 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";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the bucket name. Example: examplebucket. 
var bucketName = "examplebucket";
// Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
var objectName = "exampledir/exampleobject.txt";
// Create an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Delete the object. 
    client.DeleteObject(bucketName, objectName);
    Console.WriteLine("Delete object succeeded");
}
catch (Exception ex)
{
    Console.WriteLine("Delete object failed. {0}", ex.Message);
}

Delete multiple objects

You can delete up to 1,000 objects at a time.

The result can be returned in one of the following modes:
  • verbose: If quietMode is not specified or is set to false, a list of all deleted objects is returned. This is the default return mode.
  • quiet: If quietMode is set to true, no message body is returned.

The following code provides an example on how to delete multiple specified objects from a bucket named examplebucket:

using Aliyun.OSS;
// 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";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the bucket name. Example: examplebucket. 
var bucketName = "examplebucket";
// Create an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Specify the paths of multiple objects that you want to delete. The full paths of the objects cannot contain the bucket name. 
    var keys = new List<string>();
    keys.Add("exampleobject.txt");
    keys.Add("testdir/sampleobject.txt");
    // Leave quietMode empty or set quietMode to false to return the list of deleted objects. 
    var quietMode = false;
    var request = new DeleteObjectsRequest(bucketName, keys, quietMode);
    // Delete multiple 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);
}

References

  • Delete a single object

    For more information about the API operation that you can call to delete a single object, see DeleteObject.

  • Delete multiple objects
    • For the complete sample code that is used to delete multiple objects, visit GitHub.
    • For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.