This topic describes how to delete a single or multiple objects and objects whose names contain a specified prefix from a versioning-enabled bucket.

Delete a single object

If you do not specify the version ID when you perform the DeleteObject operation on an object in a versioning-enabled bucket, the current version of the object is retained. A delete marker is added and becomes the current version of the object. If you specify the version ID, the specified version of the object is permanently deleted.

  • If you do not specify the version ID:

    By default, the DeleteObject operation is performed on the current version of the target object. The current version is retained and a delete marker is inserted as the new current version of the object. When the GetObject operation is performed on the object, and OSS identifies that the current version of the object is a delete marker, OSS returns 404 Not Found. Additionally, the header: x-oss-delete-marker = true and new version ID x-oss-version-id of the delete marker are included in the response.

    If the value of the x-oss-delete-marker header is true, the version specified in the x-oss-version-id field is a delete marker.

  • If you specify the version ID:

    OSS permanently deletes the object with the specified version ID. For example, if you want to delete an object with a version ID of "null", you must add params['versionId'] = “null” when you set the params parameter. OSS identifies the string "null" as a version ID and deletes the object whose version ID is "null".

The following section provides examples on how to permanently delete a specified version of an object and how to temporarily delete an object:
  • Permanently delete an object with a specified version ID

    The following code provides an example on how to permanently delete a specified version of an object:

    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
    {
        // You can specify all versions of the object, including the delete markers.
        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 without specifying the version ID

    The following code provides an example on how to temporarily delete an object:

    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
    {
        // This method temporarily delete an object in a versioning-enabled bucket. After the object is deleted, a delete marker is added.
        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 deleting a single object, see DeleteObject.

Delete multiple objects

You can call the DeleteObjectVersions operation to delete multiple objects from a versioning-enabled bucket. If you do not specify the version IDs in your request, delete markers are added. If you specify the version IDs in your request, objects with the specified version IDs are permanently deleted.

The following section provides examples on how to delete multiple objects without specifying the version ID or by specifying the version ID:

  • Delete multiple objects without specifying the version IDs

    The following code provides an example on how to delete multiple objects without specifying the version ID:

    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;
        // The third parameter in DeleteObjectsRequest specifies the return mode.
        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);
    }
  • Delete multiple objects by specifying the version IDs

    The following code provides an example on how to delete multiple objects by specifying the version IDs or delete markers by specifying the version IDs:

    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 object with the specified version ID or the delete marker with the specified version ID.
        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 deleting multiple objects, see DeleteMultipleObjects.

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 a specified prefix and delete the objects.
    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);
}