All Products
Search
Document Center

Object Storage Service:Delete files (Go SDK V1)

Last Updated:Nov 28, 2025

For buckets with versioning enabled, OSS supports soft deletes and permanent deletes for objects. The type of deletion depends on whether you specify a `versionId`. This feature lets you delete a single object, multiple objects, or objects that have a specific prefix.

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 Configure OSSClient instances.

  • 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 with versioning

This section describes the deletion behavior for objects when versioning is enabled.

  • If you do not specify a versionId (soft delete):

    If you perform a delete operation without specifying a `versionId`, the current version of the object is not deleted. Instead, OSS inserts a delete marker for the current version. When you perform a `GetObject` operation, OSS detects that the current version is a delete marker and returns 404 Not Found. The response also includes the header x-oss-delete-marker = true and the version number of the new delete marker in x-oss-version-id.

    A value of `true` for x-oss-delete-marker indicates that the version corresponding to the returned x-oss-version-id is a delete marker.

    For more information about how to restore a soft-deleted object, see Restore files.

  • If you specify a versionId (permanent delete):

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

Sample code

Delete a single file

The following code examples show how to permanently delete and soft delete a single object.

Delete an object by specifying a versionId

The following code shows how to permanently delete an object by specifying a `versionId`:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set yourBucketName to 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)
	}

	// Set youObjectName to the full path of the object. Do not include the bucket name.
	// Set yourObjectVersionId to the versionId of the object. After you specify the versionId, the object of this version 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 a versionId

The following code shows how to soft delete an object without specifying a `versionId`:

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 this 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.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set yourBucketName to 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)
	}

	// Soft delete the object without specifying a versionId. This operation adds a delete marker to the object.
	// Set youObjectName to the full path of the object. Do not include the bucket name.
	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)
	}

	// Print the delete marker information.
	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 files

The following code examples show how to permanently delete and soft delete multiple objects or delete markers.

Permanently delete multiple objects by specifying versionIds

The following code shows how to permanently delete multiple objects by specifying their `versionId`s:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set yourBucketName to 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 versionIds of the objects to be permanently deleted.
	keyArray := []oss.DeleteObject{
		{Key: "objectName1", VersionId: "objectVersionId1"},
		{Key: "objectName2", VersionId: "objectVersionId2"},
	}

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

	// Print 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.")
}

Permanently delete multiple delete markers by specifying versionIds

The following code shows how to permanently delete multiple delete markers by specifying their `versionId`s:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set yourBucketName to 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 information of the delete markers.
	keyArray := []oss.DeleteObject{
		{Key: "objectName1", VersionId: "objectVersionId1"},
		{Key: "objectName2", VersionId: "objectVersionId2"},
	}

	// Permanently delete multiple delete markers by specifying their versionIds.
	ret, err := bucket.DeleteObjectVersions(keyArray)
	if err != nil {
		log.Fatalf("Failed to delete objects: %v", err)
	}

	// Print the information about the deleted objects and delete 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.")
}

Soft delete multiple objects without specifying versionIds

The following code shows how to soft delete multiple objects without specifying their `versionId`s:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set yourBucketName to 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)
	}

	// Soft delete multiple objects without specifying their versionIds. This operation adds delete markers to the objects.
	keyArray := []oss.DeleteObject{
		{Key: "objectName1"},
		{Key: "objectName2"},
	}

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

	// Print the delete markers of 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 to delete a single object, see DeleteObject.

  • For more information about the API operation to delete a specified version of an object, see DeleteObjectVersions.