Use the DeleteVectors operation to delete vectors from a vector bucket by key. Deletion is permanent and cannot be undone.
Warning
Before deleting vectors, verify the target keys to avoid unintended data loss.
Permissions
An Alibaba Cloud account has all permissions by default. Resource Access Management (RAM) users and RAM roles have no permissions by default. Grant the required permissions using a RAM policy or a bucket policy.
| API | Action | Description |
|---|---|---|
| DeleteVectors | oss:DeleteVectors | Deletes vector data. |
Method definition
func (c *VectorsClient) DeleteVectors(ctx context.Context, request *DeleteVectorsRequest, optFns ...func(*oss.Options)) (*DeleteVectorsResult, error)Request parameters
| Parameter | Type | Description |
|---|---|---|
| ctx | context.Context | The request context. |
| request | *DeleteVectorsRequest | The request parameters. These include the bucket name, index name, and a list of vector keys. For more information, see DeleteVectorsRequest. |
| optFns | ...func(*Options) | (Optional) The operation-level configuration parameters. For more information, see Options. |
Return values
| Parameter | Type | Description |
|---|---|---|
| result | *DeleteVectorsResult | The return value. For more information, see DeleteVectorsResult. |
| err | error | The error message. This is nil if the operation is successful. |
Sample code
The following example deletes two vectors by key from a specified index in a vector bucket.
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() {
// Define command-line parameters.
flag.StringVar(®ion, "region", "", "The region in which the vector bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
flag.StringVar(&accountId, "account-id", "", "The id of vector account.")
}
func main() {
// Parse command-line parameters.
flag.Parse()
// Verify required parameters.
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")
}
// Create a configuration and set the credential provider, region, and account ID.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
// Create a vector store client.
client := vectors.NewVectorsClient(cfg)
// Construct a request to delete vector data.
request := &vectors.DeleteVectorsRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr("index"),
Keys: []string{
"key1", "key2", // The list of vector keys to delete.
},
}
// Execute the operation to delete vector data.
result, err := client.DeleteVectors(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete vectors %v", err)
}
// Print the operation result.
log.Printf("delete vectors result:%#v\n", result)
}Usage notes
Deleted vectors are permanently removed and cannot be restored.
References
For the complete sample code, see delete_vectors.go.