Use the Go SDK V2 to call the DeleteVectorBucket operation and permanently delete a vector bucket.
This operation is irreversible. Once deleted, the vector bucket and all its data cannot be recovered.
Prerequisites
Before you begin, make sure that you have:
Go SDK V2 installed and configured
The
oss:DeleteVectorBucketpermission granted to your identity — Alibaba Cloud accounts have this permission by default; Resource Access Management (RAM) users and RAM roles do not, and must be granted it via a RAM policy or a bucket policyThe name and region of the vector bucket to delete
The account ID associated with the vector bucket
Method definition
func (c *VectorsClient) DeleteVectorBucket(ctx context.Context, request *DeleteVectorBucketRequest, optFns ...func(*Options)) (*DeleteVectorBucketResult, error)Parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. |
request | *DeleteVectorBucketRequest | The request parameters, including the name of the vector bucket to delete. See DeleteVectorBucketRequest. |
optFns | ...func(*Options) | (Optional) Interface-level configuration options. See Options. |
Return values
| Value | Type | Description |
|---|---|---|
result | *DeleteVectorBucketResult | The result of the operation. Valid only when err is nil. See DeleteVectorBucketResult. |
err | error | The error returned if the operation fails. nil on success. Check err != nil to handle failures. |
Example
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"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)
var (
region string
bucketName string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region where the vector bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
flag.StringVar(&accountId, "account-id", "", "The ID of the vector account.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, accountId required")
}
// Load credentials from environment variables, region and account ID from flags
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId)
// Initialize the VectorsClient
client := vectors.NewVectorsClient(cfg)
// Build the delete request
request := &vectors.DeleteVectorBucketRequest{
Bucket: oss.Ptr(bucketName),
}
// Call DeleteVectorBucket and handle the result
result, err := client.DeleteVectorBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete vector bucket %v", err)
}
log.Printf("delete vector bucket result:%#v\n", result)
}Run the example with the required flags:
go run main.go --region <region-id> --bucket <bucket-name> --account-id <account-id>Replace the placeholders with actual values:
| Placeholder | Description | Example |
|---|---|---|
<region-id> | The region where the vector bucket is located | cn-hangzhou |
<bucket-name> | The name of the vector bucket to delete | my-vector-bucket |
<account-id> | The ID of the vector account | 123456789 |
References
For the complete sample file, see delete_vector_bucket.go.