This topic describes how to delete objects from a bucket with versioning enabled or suspended.

Delete a single object

If you do not specify the versionId when performing the delete_object operation on an object in a bucket with versioning enabled, the current version of the object is kept and a delete marker is added as the current version of the object. If you specify the versionId, the specified version of the object is permanently deleted.
Note
  • If the versionId is not specified when you perform the DeleteObject operation:

    The operation is performed on the current version of the target object by default. However, the current version is kept but not deleted. A delete marker is added to the object as the new current version of the object. The x-oss-delete-marker = true and x-oss-version-id (indicating the version ID of the generated delete marker) headers are included in the response. When a GetObject operation is performed on the object, OSS identifies that the current version of the object is a delete marker and returns the 404 Not Found error.

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

  • If the versionId is specified when you perform the DeleteObject operation:

    OSS permanently deletes the version specified by the versionId. For example, if you want to delete a version of which the ID is null, specify the value of versionId in the request as "null". OSS recognizes the string "null" as a version ID and deletes the version with it.

Examples of deleting a specified object version permanently and deleting an object temporarily are provided as follows:

  • Delete a version of the object permanently.
    You can run the following code to delete a specified version of an object permanently:
    // This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String bucketName = "<yourBucketName>";
    String objectName = "<yourObjectName>";
    // Inputs the version ID of the object or delete marker to be deleted.
    String versionid = "<yourObjectVersionidOrDelMarkerVersionid>";
    
    // Creates an OSSClient instance.
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Deletes the specified version of the object.
    ossClient.deleteVersion(bucketName, objectName , versionid);
    
    // Close the OSSClient instance.
    ossClient.shutdown();
  • Delete an object temporarily.
    You can run the following code to delete an object temporarily:
    // This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String bucketName = "<yourBucketName>";
    String objectName = "<yourObjectName>";
    
    // Creates an OSSClient instance.
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    // If versioning is enabled for the bucket, this method deletes the object temporarily and adds a delete marker to the object.
    ossClient.deleteObject(bucketName, objectName);
    
    // Close the OSSClient instance.
    ossClient.shutdown();

For more information about deleting a single object, see DeleteObject.

Delete multiple objects

You can use batch_delete_objects to delete multiple objects in a bucket with versioning enabled. If you do not specify the versionId in the request, delete markers are added to the objects that you want to delete. If you specify the versionId in the request, the specified versions of the objects that you want to delete are permanently deleted.

  • Delete multiple objects by sending a request without specifying the version IDs.
    You can run the following code to delete multiple objects without specifying a versionId:
    // This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String bucketName = "<yourBucketName>";
    String versionid = "<yourObjectVersionid>";
    
    // Creates an OSSClient instance.
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Deletes multiple objects.
    List<String> KeysList = new ArrayList<String>();
    KeysList.add("<yourObject1Name>");
    KeysList.add("<yourObject2Name>");
    DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName);
    request.setVersionId(versionid);
    request.setKeys(KeysList);
    // Initiates a deleteObjects request.
    DeleteObjectsResult delObjResult = ossClient.deleteObjects(request);
    
    // Views the deletion results.
    for (String o : delObjResult.getDeletedObjects()) {
        String keyName = URLDecoder.decode(o, "UTF-8");
        System.out.println("delete key name: " + keyName);
    }
    
    // Closes the OSSClient instance.
    ossClient.shutdown();
  • Delete multiple objects by specifying the version IDs in the request.
    You can run the following code to delete multiple objects or delete markers with a specified version ID:
    // This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    String bucketName = "<yourBucketName>";
    
    
    // Creates an OSSClient instance.
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Deletes the objects and delete markers with the specified version IDs.
    List<KeyVersion> keyVersionsList = new ArrayList<KeyVersion>();
    keyVersionsList.add(new KeyVersion("<yourObject3Name>","<yourObject3NameVersionid>"));
    keyVersionsList.add(new KeyVersion("<yourObject4Name>","<yourObject4DelMarkerVersionid>"));
    DeleteVersionsRequest delVersionsRequest = new DeleteVersionsRequest(bucketName);
    delVersionsRequest.setKeys(keyVersionsList);
    // Initiates a deleteVersions request.
    DeleteVersionsResult delVersionsResult = ossClient.deleteVersions(delVersionsRequest);
    
    // Views the deletion results.
    for (DeletedVersion delVer : delVersionsResult.getDeletedVersions()) {
      String keyName = URLDecoder.decode(delVer.getKey(), "UTF-8");
      String keyVersionId = delVer.getVersionId();
      String keyDelMarkerId = delVer.getDeleteMarkerVersionId();
      System.out.println("delete key: " + keyName);
      System.out.println("delete key versionid: " + keyVersionId);
      if(keyDelMarkerId != null && keyDelMarkerId.length() != 0){
        System.out.println("delete key del_marker versionid: " + keyDelMarkerId);
      }
    }
    
    // Closes the OSSClient instance.
    ossClient.shutdown();

For more information about deleting multiple objects, see DeleteMultipleObjects.