All Products
Search
Document Center

Object Storage Service:Delete objects (Go SDK v2)

Last Updated:Apr 03, 2026

In versioning-enabled buckets, OSS lets you soft-delete or permanently delete an object depending on whether you provide a versionId in the request. You can delete a single object, multiple objects, or objects that share a specific prefix.

Usage notes

  • The code examples in this topic use the region ID cn-hangzhou for the China (Hangzhou) region and demonstrate access over the public endpoint. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint. For a complete list of OSS regions and endpoints, see Regions and endpoints.

  • The examples in this topic read access credentials from environment variables. For information about how to configure access credentials, see Configure access credentials.

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

Deletion behavior in a versioning-enabled bucket

The deletion behavior in a versioning-enabled bucket depends on whether you specify a versionId.

  • Without specifying a versionId (soft delete):

    If you send a delete request without a versionId, OSS does not permanently delete the object. Instead, it creates a delete marker, which becomes the current version of the object. When you retrieve an object that has a delete marker as its current version, OSS returns a404 Not Found error. The response includes thex-oss-delete-marker: true header and thex-oss-version-id header, which contains the versionId of the new delete marker. A value of true forx-oss-delete-marker indicates that the version associated with the returnedx-oss-version-id is a delete marker.

    To learn how to restore a soft-deleted object, see Recover files.

  • Specifying a versionId (permanent delete):

    If you specify a versionId in the params parameter during a delete operation, OSS permanently deletes that version. To delete the version whose ID is "null", add params['versionId'] = "null" to the params parameter.

Examples

Delete a single object

Delete with versionId

The following code shows how to permanently delete a specific version of an object by providing its versionId.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
	objectName string // The name of the object.
)

// init is used to initialize command-line flags.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	// Parse command-line flags.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check if the object name is provided.
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Load the default configuration and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to delete the object.
	request := &oss.DeleteObjectRequest{
		Bucket:    oss.Ptr(bucketName),      // The name of the bucket.
		Key:       oss.Ptr(objectName),      // The name of the object.
		VersionId: oss.Ptr("yourVersionId"), // Replace with the actual versionId of the object.
	}

	// Execute the delete operation and handle the result.
	result, err := client.DeleteObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete object %v", err)
	}

	// Print the result.
	log.Printf("delete object result:%#v\n", result)
}

Delete without versionId

The following code shows how to soft-delete an object.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
	objectName string // The name of the object.
)

// init is used to initialize command-line flags.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	// Parse command-line flags.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check if the object name is provided.
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Load the default configuration and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to delete the object.
	request := &oss.DeleteObjectRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Key:    oss.Ptr(objectName), // The name of the object.
	}

	// Execute the delete operation and handle the result.
	result, err := client.DeleteObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete object %v", err)
	}

	// Print the result.
	log.Printf("delete object result:%#v\n", result)
}

Delete multiple objects

Delete with versionIds

The following code shows how to permanently delete multiple object versions in a single request.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// init is used to initialize command-line flags.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line flags.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Define the list of objects to delete.
	DeleteObjects := []oss.DeleteObject{
		{Key: oss.Ptr("object1"), VersionId: oss.Ptr("versionId1")}, // Replace with the key and versionId of the first object.
		{Key: oss.Ptr("object2"), VersionId: oss.Ptr("versionId2")}, // Replace with the key and versionId of the second object.
		{Key: oss.Ptr("object3"), VersionId: oss.Ptr("versionId3")}, // Replace with the key and versionId of the third object.
	}

	// Create a request to delete multiple objects.
	request := &oss.DeleteMultipleObjectsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Delete: &oss.Delete{
			Objects: DeleteObjects, // The list of objects to delete.
		},
	}

	// Execute the delete operation and handle the result.
	result, err := client.DeleteMultipleObjects(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete multiple objects %v", err)
	}

	// Print the result.
	log.Printf("delete multiple objects result:%#v\n", result)
}

Delete without versionIds

The following code shows how to soft-delete multiple objects in a single request.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// init is used to initialize command-line flags.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line flags.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Define the list of objects to delete.
	DeleteObjects := []oss.DeleteObject{
		{Key: oss.Ptr("object1")}, // Replace with the key of the first object.
		{Key: oss.Ptr("object2")}, // Replace with the key of the second object.
		{Key: oss.Ptr("object3")}, // Replace with the key of the third object.
	}

	// Create a request to delete multiple objects.
	request := &oss.DeleteMultipleObjectsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Delete: &oss.Delete{
			Objects: DeleteObjects, // The list of objects to delete.
		},
	}

	// Execute the delete operation and handle the result.
	result, err := client.DeleteMultipleObjects(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete multiple objects %v", err)
	}

	// Print the result.
	log.Printf("delete multiple objects result:%#v\n", result)
}

Related documentation

  • For more information about the DeleteObject API operation, see DeleteObject.

  • For more information about the DeleteMultipleObjects API operation, see DeleteMultipleObjects.