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 specified version of the object permanently.

    You can run the following code to delete a specified version of an object permanently:

    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Creates an OSSClient.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtains the bucket.
      bucket, err := client.Bucket("<yourBucketName>")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Deletes the specified version of the object permanently.
      err = bucket.DeleteObject(key, oss.VersionId("yourObjectVersionId"))
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    }
  • Delete an object temporarily.
    Run the following code to delete an object temporarily:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Creates an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtains the bucket.
      bucket, err := client.Bucket("<yourBucketName>")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Deletes the object temporarily by sending a request without specifying the version ID.
      err = bucket.DeleteObject("youObjectName", oss.GetResponseHeader(&retHeader))
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Prints the information about the delete markers.
      fmt.Println("x-oss-version-id:", oss.GetVersionId(retHeader))
      fmt.Println("x-oss-delete-marker:", oss.GetDeleteMark(retHeader))
    }

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 versionId.
    You can run the following code to delete multiple objects without specifying a versionId:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Creates an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtains the bucket.
      bucket, err := client.Bucket("yourBucketName")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Deletes the objects without specifying version IDs.
      keyArray := []oss.DeleteObject{
        oss.DeleteObject{Key: "objectName1"},
        oss.DeleteObject{Key: "objectName2"},
      }
    
      res, err := bucket.DeleteObjectVersions(keyArray)
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
      // Prints the delete markers of the deleted objects.
      for _, object := range res.DeletedObjectsDetail {
        fmt.Println("key:", object.Key, "\n DeleteMarker:", object.DeleteMarker, "\nDeleteMarkerVersionId", object.DeleteMarkerVersionId)
      }
    }
  • Delete multiple objects by specifying the version IDs in the request.
    Run the following code to delete multiple objects or delete markers with specified version IDs:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Creates an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtains the bucket.
      bucket, err := client.Bucket("yourBucketName")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Specifies the version IDs to delete the objects permanently.
      keyArray := []oss.DeleteObject{
        oss.DeleteObject{Key: "objectName1", VersionId:"objectVersionId1"},
        oss.DeleteObject{Key: "objectName2", VersionId:"objectVersionId2"},
      }
    
      // Deletes the objects with the specified version IDs.
      ret, err := bucket.DeleteObjectVersions(keyArray)
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
      // Prints the deleted objects.
      for _, object := range ret.DeletedObjectsDetail {
        fmt.Println("Key:", object.Key, "\n VersionId:", object.VersionId)
      }
    }
  • Delete multiple delete markers by specifying version IDs.
    You can run the following code to delete multiple delete markers by specifying version IDs:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Creates an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtains the bucket.
      bucket, err := client.Bucket("yourBucketName")
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Specifies the version IDs of the objects.
      keyArray := []oss.DeleteObject{
        oss.DeleteObject{Key: "objectName1", VersionId:"objectVersionId1"},
        oss.DeleteObject{Key: "objectName2", VersionId:"objectVersionId2"},
      }
    
      // Deletes the delete markers by specifying the version IDs.
      ret, err := bucket.DeleteObjectVersions(keyArray)
      if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
      // Prints the deleted objects and delete markers.
      for _, object := range ret.DeletedObjectsDetail {
        fmt.Println("key:", object.Key, "\n versionId:", object.VersionId, "\n DeleteMarker:", object.DeleteMarker, "\nDeleteMarkerVersionId", object.DeleteMarkerVersionId)
      }
    }

For more information about deleting multiple objects, see DeleteMultipleObjects.