All Products
Search
Document Center

Object Storage Service:Delete objects

Last Updated:Sep 29, 2024

You can temporarily or permanently delete an object from a versioning-enabled bucket based on whether you specify the version ID of the object in the request. In addition, you can temporarily or permanently delete an object, multiple objects, or objects whose names contain a specific prefix from a versioning-enabled bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To delete an object, you must have the oss:DeleteObject permission. For more information, see Attach a custom policy to a RAM user.

Delete operations on a versioning-enabled bucket

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

  • Delete an object without specifying a version ID (temporary deletion)

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

    If the value of x-oss-delete-marker is true, the value of x-oss-version-id is the version ID of a delete marker.

    For more information about how to restore a deleted object, see Restore objects.

  • Delete an object by specifying a version ID (permanent deletion)

    If you specify the version ID of the object that you want to delete in the request, OSS permanently deletes the specified version of the object based on the versionId parameter specified in params. To delete the version whose ID is null, add params['versionId'] = "null" to params. OSS identifies the string "null" as the ID of the version to permanently delete and deletes the version whose ID is null.

Sample code

Delete an object

The following examples show how to permanently or temporarily delete an object from a versioning-enabled bucket.

Delete an object by specifying its version ID

The following sample code provides an example on how to permanently delete an object from a versioning-enabled bucket by specifying its version ID:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	endpoint := "yourEndpoint"
	client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Specify the full path of the object. Do not include the bucket name in the full path. 
	// Specify the version ID of the object.If this parameter is specified, the object with the specified version ID is permanently deleted. 
	objectName := "yourObjectName"
	objectVersionId := "yourObjectVersionId"
	err = bucket.DeleteObject(objectName, oss.VersionId(objectVersionId))
	if err != nil {
		log.Fatalf("Failed to delete object '%s' with version ID '%s': %v", objectName, objectVersionId, err)
	}

	log.Println("Object deleted successfully.")
}

Delete an object without specifying its version ID

The following sample code provides an example on how to temporarily delete an object from a versioning-enabled bucket without specifying its version ID:

package main

import (
	"log"
	"net/http"

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

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	endpoint := "yourEndpoint"
	client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Temporarily delete an object without specifying its version ID. A delete marker is added to the object. 
	// Specify the full path of the object. Do not include the bucket name in the full path. 
	objectName := "yourObjectName"
	var retHeader http.Header
	err = bucket.DeleteObject(objectName, oss.GetResponseHeader(&retHeader))
	if err != nil {
		log.Fatalf("Failed to delete object '%s': %v", objectName, err)
	}

	// Display the information about the delete marker. 
	log.Printf("x-oss-version-id: %s", oss.GetVersionId(retHeader))
	log.Printf("x-oss-delete-marker: %t", oss.GetDeleteMark(retHeader))

	log.Println("Object deleted successfully.")
}

Delete multiple objects

The following examples show how to permanently or temporarily delete multiple objects or delete markers from a versioning-enabled bucket.

Delete multiple objects by specifying their version IDs

The following sample code provides an example on how to permanently delete multiple objects from a versioning-enabled bucket by specifying their version IDs:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	endpoint := "yourEndpoint"
	client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Specify the version IDs of the objects that you want to delete permanently. 
	keyArray := []oss.DeleteObject{
		{Key: "objectName1", VersionId: "objectVersionId1"},
		{Key: "objectName2", VersionId: "objectVersionId2"},
	}

	// Delete the objects with the specified version IDs. 
	ret, err := bucket.DeleteObjectVersions(keyArray)
	if err != nil {
		log.Fatalf("Failed to delete objects: %v", err)
	}

	// Display the information about the deleted objects. 
	for _, object := range ret.DeletedObjectsDetail {
		log.Printf("Key: %s, VersionId: %s", object.Key, object.VersionId)
	}

	log.Println("Objects deleted successfully.")
}

Delete multiple delete markers by specifying their version IDs

The following sample code provides an example on how to permanently delete multiple delete markers from a versioning-enabled bucket by specifying their version IDs:

package main

import (
    "log"
    "os"

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

func main() {
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        log.Fatalf("Failed to create credentials provider: %v", err)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    endpoint := "yourEndpoint"
    client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        log.Fatalf("Failed to create OSS client: %v", err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket, err := client.Bucket(bucketName)
    if err != nil {
        log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
    }

    // Specify the version IDs of the delete markers that you want to delete. 
    keyArray := []oss.DeleteObject{
        {Key: "objectName1", VersionId: "objectVersionId1"},
        {Key: "objectName2", VersionId: "objectVersionId2"},
    }

    // Delete the specified markers. 
    ret, err := bucket.DeleteObjectVersions(keyArray)
    if err != nil {
        log.Fatalf("Failed to delete objects: %v", err)
    }

    // Display the information about the deleted objects and markers.
    for _, object := range ret.DeletedObjectsDetail {
        log.Printf("Key: %s, VersionId: %s, DeleteMarker: %t, DeleteMarkerVersionId: %s",
            object.Key, object.VersionId, object.DeleteMarker, object.DeleteMarkerVersionId)
    }

    log.Println("Objects deleted successfully.")
}

Delete multiple objects without specifying their version IDs

The following sample code provides an example on how to temporarily delete multiple objects from a versioning-enabled bucket without specifying their version IDs:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	endpoint := "yourEndpoint"
	client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Temporarily delete multiple objects without specifying their version IDs. Delete markers are added to the objects. 
	keyArray := []oss.DeleteObject{
		{Key: "objectName1"},
		{Key: "objectName2"},
	}

	// Delete the objects.
	res, err := bucket.DeleteObjectVersions(keyArray)
	if err != nil {
		log.Fatalf("Failed to delete objects: %v", err)
	}

	// Display the information about the delete markers that are added to the objects. 
	for _, object := range res.DeletedObjectsDetail {
		log.Printf("Key: %s, DeleteMarker: %t, DeleteMarkerVersionId: %s",
			object.Key, object.DeleteMarker, object.DeleteMarkerVersionId)
	}

	log.Println("Objects deleted successfully.")
}

References

  • For more information about the API operation that you can call to delete an object, see DeleteObject.

  • For more information about the API operation that you can call to delete objects with specified version IDs, see DeleteObjectVersions.