This topic describes how to delete a single object, multiple objects, or objects whose names contain a specified prefix from a versioned bucket.

Delete operations in versioned bucket

When you delete an object from a versioned bucket, you must determine whether to specify a version ID in the request.

  • Do not specify a version ID (temporary deletion)

    By default, if you do not specify a version ID in a DeleteObject request, OSS does not delete the current version of the object but add a delete marker to the object as the new current version. If you perform the GetObject operation on the object, OSS identifies that the current version of the object is a delete marker, and then returns 404 Not Found. In addition, the x-oss-delete-marker header and the x-oss-version-id header that indicates the version ID of the created delete marker are included in the response to the DeleteObject request.

    The value of the x-oss-delete-marker header in the response is true, which indicates that the x-oss-version-id is the version ID of a delete marker.

  • Specify a version ID (permanent deletion)

    If you specify a version ID in a DeleteObject request, OSS permanently deletes the version whose ID is specified by the versionId field in the params parameter. To delete a version whose ID is null, add params['versionId'] = "null" in params in the request. OSS identifies the string "null" as the ID of the version to delete, and deletes the version whose ID is null.

Delete a single object

The following examples describe how to permanently or temporarily delete a single object from a versioned bucket.

  • Permanently delete an object from a versioned bucket

    The following code provides an example on how to permanently delete an object with a specified version ID from a versioned bucket:

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    var objectName = "<yourObjectName>";
    var versionid = "<yourObjectVersionidOrDelMarkerVersionid>";
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        // Specify the version ID of the object or delete marker you want to delete.
        var request = new DeleteObjectRequest(bucketName, key)
        {
            VersionId = versionid
        };
        client.DeleteObject(request);
        Console.WriteLine("Delete object succeeded");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Delete object failed. {0}", ex.Message);
    }
  • Temporarily delete an object from a versioned bucket

    The following code provides an example on how to temporarily delete an object from a versioned bucket:

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    var objectName = "<yourObjectName>";
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    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);
    }

For more information about how to delete a single object, see DeleteObject.

Delete multiple objects

The following examples describe how to permanently or temporarily delete multiple objects from a versioned bucket.

  • Permanently delete multiple objects from a versioned bucket

    The following code provides an example on how to permanently delete multiple objects or delete markers with specified version IDs from a versioned bucket:

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    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);
    }

    For more information about how to delete multiple objects, see DeleteMultipleObjects.

  • Temporarily delete multiple objects from a versioned bucket

    The following code provides an example on how to temporarily delete multiple objects from a versioned bucket:

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    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, a list of the objects that failed to be deleted is returned. False indicates verbose. In this mode, a list of the deleted objects is 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);
    }

Delete objects whose names contain a specified prefix

The following code provides an example on how to delete objects whose names contain a specified prefix:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var prefix = "<yourkeyPrefix>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{   
    ObjectVersionList result = null;
    var request = new ListObjectVersionsRequest(bucketName)
    {
        // Specify a prefix.
        Prefix = prefix;
    };

    // List the versions of all objects whose names contain the specified 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);
}