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 by specifying its version ID

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

    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Create an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtain the bucket.
      bucket, err := client.Bucket("<yourBucketName>")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Permanently delete an object by specifying the version ID of the object.
      err = bucket.DeleteObject(key, oss.VersionId("yourObjectVersionId"))
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    }
  • Temporarily delete an object without specifying its version ID
    The following code provides an example on how to temporarily delete an object from a versioned bucket:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Create an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtain the bucket.
      bucket, err := client.Bucket("<yourBucketName>")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Temporarily delete an object without specifying its version ID. A delete marker is added to the object.
      err = bucket.DeleteObject("youObjectName", oss.GetResponseHeader(&retHeader))
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Display the information about the delete marker added to the object.
      fmt.Println("x-oss-version-id:", oss.GetVersionId(retHeader))
      fmt.Println("x-oss-delete-marker:", oss.GetDeleteMark(retHeader))
    }

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 or delete markers from a versioned bucket.

  • Permanently delete multiple objects by specifying their version IDs
    The following code provides an example on how to permanently delete multiple objects with specified version IDs from a versioned bucket:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Create an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtain the bucket.
      bucket, err := client.Bucket("yourBucketName")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Specify the version IDs of the objects you want to delete.
      keyArray := []oss.DeleteObject{
        oss.DeleteObject{Key: "objectName1", VersionId:"objectVersionId1"},
        oss.DeleteObject{Key: "objectName2", VersionId:"objectVersionId2"},
      }
    
      // Delete the object with the specified version ID.
      ret, err := bucket.DeleteObjectVersions(keyArray)
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
      // Display the information about the deleted objects.
      for _, object := range ret.DeletedObjectsDetail {
        fmt.Println("Key:", object.Key, "\n VersionId:", object.VersionId)
      }
    }
  • Permanently delete multiple delete markers by specifying their version IDs
    The following code provides an example on how to permanently delete multiple delete markers with specified version IDs from a versioned bucket:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Create an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtain the bucket.
      bucket, err := client.Bucket("yourBucketName")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Specify the version IDs of the objects that you want to delete.
      keyArray := []oss.DeleteObject{
        oss.DeleteObject{Key: "objectName1", VersionId:"objectVersionId1"},
        oss.DeleteObject{Key: "objectName2", VersionId:"objectVersionId2"},
      }
    
      // Permanently delete the delete markers with the specified version IDs.
      ret, err := bucket.DeleteObjectVersions(keyArray)
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
      // Display 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)
      }
    }
  • Temporarily delete multiple objects without specifying their version IDs
    The following code provides an example on how to temporarily delete multiple objects from a versioned bucket:
    package main
    
    import (
      "fmt"
      "net/http"
      "os"
      "strings"
    
      "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
      // Create an OSSClient instance.
      client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Obtain the bucket.
      bucket, err := client.Bucket("yourBucketName")
      if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
      }
    
      // Temporarily delete multiple object without specifying their version IDs. Delete markers are added to the objects.
      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)
      }
      // Display the delete markers that are added to the objects.
      for _, object := range res.DeletedObjectsDetail {
        fmt.Println("key:", object.Key, "\n DeleteMarker:", object.DeleteMarker, "\nDeleteMarkerVersionId", object.DeleteMarkerVersionId)
      }
    }

For more information about how to delete 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:

package main

import (
    "fmt"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Obtain the bucket.
    bucket, err := client.Bucket("<yourBucketName>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // List the objects whose name contain the specified prefix and delete these objects.
    prefix := oss.Prefix("<yourObjectPrefix>")
    marker := oss.KeyMarker("")
    version := oss.VersionIdMarker("")

    for {
        lor, err := bucket.ListObjectVersions(marker, prefix, version)
        if err ! = nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }

        objects := []oss.DeleteObject{}
        for _, object := range lor.ObjectDeleteMarkers {
            objects = append(objects, oss.DeleteObject{
                Key:       object.Key,
                VersionId: object.VersionId,
            })
        }

        for _, object := range lor.ObjectVersions {
            objects = append(objects, oss.DeleteObject{
                Key:       object.Key,
                VersionId: object.VersionId,
            })
        }

        delRes, err := bucket.DeleteObjectVersions(objects, oss.DeleteObjectsQuiet(true))
        if err ! = nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }

        if len(delRes.DeletedObjectsDetail) > 0 {
            fmt.Println("these objects deleted failure,", delRes.DeletedObjectsDetail)
            os.Exit(-1)
        }

        prefix = oss.Prefix(lor.Prefix)
        marker = oss.KeyMarker(lor.NextKeyMarker)
        version = oss.VersionIdMarker(lor.NextVersionIdMarker)
        if ! lor.IsTruncated {
            break
        }
    }
    fmt.Printf("delete success\n")
}