删除索引

最近更新时间:2026-04-01 16:42:21

我的收藏

简介

本文档介绍如何使用对象存储 COS 的 Go SDK 删除指定的向量索引。

功能说明

DeleteIndex 方法用于删除向量存储桶中指定的向量索引。
注意:
删除索引会同时删除该索引中存储的所有向量数据
此删除操作不可逆,请谨慎操作。
删除某一个索引不会影响桶中的其他索引或数据。

方法原型

func (s *VectorService) DeleteIndex(ctx context.Context, opt *DeleteIndexOptions) (*Response, error)

请求参数说明

DeleteIndexOptions 结构体字段说明:
参数
描述
类型
是否必填
VectorBucketName
向量桶名称,格式为 <BucketName-APPID>,例如 examplebucket-1250000000
string
IndexName
需要删除的索引名称。
string

返回结果说明

调用成功后,将返回 (*Response, error) 两个值。本方法无特殊的业务返回字段,当 error == nil 时表示删除成功。
返回值
描述
类型
resp
响应头信息,包含 HTTP 响应的详细对象等。
*cos.Response
err
错误信息。如果请求成功,返回 nil
error
如果请求失败,将返回非 nilerror。向量检索相关的业务错误会封装为 *cos.VectorErrorResponse 结构,您可以通过 cos.IsVectorError() 辅助函数判断并获取详细错误信息。详细的错误处理方式、VectorErrorResponse 结构体说明及服务端错误码列表请参见 异常处理 文档。

使用案例

package main

import (
"context"
"fmt"
"net/http"
"os"

cos "github.com/tencentyun/cos-go-sdk-v5"
)

func main() {
// 生成 Vector 基础 URL,第二个参数控制是否使用 HTTPS,建议为 true
vectorURL, _ := cos.NewVectorURL("ap-guangzhou", true)
// 初始化 Client,设置 VectorURL 及鉴权
client := cos.NewClient(&cos.BaseURL{VectorURL: vectorURL}, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_VECTORS_SECRET_ID"),
SecretKey: os.Getenv("COS_VECTORS_SECRET_KEY"),
},
})

// 向量桶名称格式必须为:BucketName-APPID
bucketName := "examplebucket-1250000000"
indexName := "my-index"

// 构造删除索引的参数
opt := &cos.DeleteIndexOptions{
VectorBucketName: bucketName,
IndexName: indexName,
}

// 调用删除索引方法
_, err := client.Vector.DeleteIndex(context.Background(), opt)
if err != nil {
if vecErr, ok := cos.IsVectorError(err); ok {
fmt.Printf("向量服务错误,错误码: %s, 错误信息: %s, 请求ID: %s\\n",
vecErr.Code, vecErr.Message, vecErr.RequestID)
} else {
fmt.Printf("请求失败: %v\\n", err)
}
return
}

fmt.Printf("成功删除向量索引: %s\\n", indexName)
}