All Products
Search
Document Center

Object Storage Service:Delete files (Go SDK V1)

Last Updated:Nov 28, 2025

You can delete a single object, multiple specified objects, objects whose names contain a specific prefix, or delete a specific directory and all objects in the directory.

Warning

Deleted objects cannot be recovered. Exercise caution when you delete objects.

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.

Delete a single file

The following sample code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	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 your bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where your bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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)
	}

	// Specify the bucket name, for example, examplebucket.
	bucketName := "examplebucket"
	// Set objectName to the full path of the file to delete, including the file extension but not the bucket name. For example, exampledir/exampleobject.txt.
	// To delete a folder, set objectName to the folder name. If the folder is not empty, you must delete all objects in the folder before you can delete the folder itself.
	objectName := "exampledir/exampleobject.txt"

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

	// Delete a single file.
	err = bucket.DeleteObject(objectName)
	if err != nil {
		log.Fatalf("Failed to delete object '%s': %v", objectName, err)
	}

	log.Printf("Successfully deleted object: %s\n", objectName)
}

Delete multiple files

You can delete up to 1,000 files in a single operation. You can delete files in one of the following ways:

  • Delete multiple files specified by name.

  • Delete all files that have a specific prefix.

  • Delete a specified folder and all the files that it contains.

OSS also supports automatic file deletion using lifecycle rules. For more information, see Lifecycle rules based on the last modified time.

Delete multiple files by name

The following code shows how to delete multiple files specified by name.

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	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 your bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where your bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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)
	}

	// Specify the bucket name, for example, examplebucket.
	bucketName := "examplebucket"

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

	// Delete multiple files and return the result.
	objectsToDelete := []string{"my-object-1", "my-object-2"}
	delRes, err := bucket.DeleteObjects(objectsToDelete)
	if err != nil {
		log.Fatalf("Failed to delete objects: %v", err)
	}
	log.Printf("Deleted Objects: %v\n", delRes.DeletedObjects)

	// Delete multiple files without returning the result.
	objectsToDeleteQuiet := []string{"my-object-3", "my-object-4"}
	_, err = bucket.DeleteObjects(objectsToDeleteQuiet, oss.DeleteObjectsQuiet(true))
	if err != nil {
		log.Fatalf("Failed to delete objects (quiet): %v", err)
	}
	log.Println("Objects deleted quietly")
}

Delete multiple files by prefix or in a folder

The following code shows how to delete multiple files that have a specific prefix or delete a specified folder and all the files that it contains.

Warning

If the prefix is not specified or is set to NULL in the following sample code, all objects in the bucket are deleted. Exercise caution when you specify a prefix in a delete operation.

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	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 your bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where your bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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)
	}

	// Specify the bucket name, for example, examplebucket.
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List and delete all files that have the specified prefix.
	marker := oss.Marker("")
	// To delete all files with the prefix "src", set the prefix to "src". This deletes all non-folder files with the "src" prefix, the "src" folder, and all files within it.
	prefix := oss.Prefix("src")
	// To delete only the "src" folder and all files within it, set the prefix to "src/".
	// prefix := oss.Prefix("src/")
	var totalDeleted int

	for {
		lor, err := bucket.ListObjects(marker, prefix)
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		objects := make([]string, len(lor.Objects))
		for i, object := range lor.Objects {
			objects[i] = object.Key
		}

		// Delete the objects.
		delRes, err := bucket.DeleteObjects(objects, oss.DeleteObjectsQuiet(true))
		if err != nil {
			log.Fatalf("Failed to delete objects: %v", err)
		}

		if len(delRes.DeletedObjects) > 0 {
			log.Fatalf("Some objects failed to delete: %v", delRes.DeletedObjects)
		}

		totalDeleted += len(objects)

		// Update the marker.
		marker = oss.Marker(lor.NextMarker)
		if !lor.IsTruncated {
			break
		}
	}

	log.Printf("Success, total deleted object count: %d\n", totalDeleted)
}

References

  • For the complete sample code for deleting files, see the GitHub sample.

  • For more information about the API operation for deleting a single file, see DeleteObject.

  • For more information about the API operation for deleting multiple files, see DeleteObjects.