You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Vector
DeleteVectors(TOS Vectors Python SDK)
Copy page
Download PDF
DeleteVectors(TOS Vectors Python SDK)
The delete_vectors interface is used to remove vectors from a specified collection. This article explains how to delete vectors using the delete_vectors interface in the TOS Vectors Python SDK.​
Notes​
  • You need to fill in the region and endpoint in the sample code based on your actual situation. For details, see Regions and Endpoints.​
  • To prevent key leakage, the sample code here reads the Access Key ID and Secret Access Key from environment variables. Therefore, before using the SDK, you need to configure long-term access credentials. For details, see Configuring Long-Term Access Credentials.​
  • TOS Vectors Python SDK supports temporary access credentials and also allows configuration of timeout mechanisms, retry strategies, and more. You can flexibly set these according to your business needs to improve application security. For details, see Common Configuration Scenarios.​
Permissions required​
The primary account has all permissions by default, while IAM users have no permissions by default. If you need a sub-account to perform the delete_vectors operation, the primary account must grant the following permissions to that sub-account through IAM policies and bucket policies:​
tosvectors:DeleteVectors​
Code example​
Batch delete vectors​
​
# Batch delete vectors.​
import os​
import tos​
​
# Retrieve AK and SK information from environment variables.​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
# Fill in your endpoint and your region with the endpoint corresponding to the bucket's region. # For example, for China (Beijing), set your endpoint to https://tosvectors-cn-beijing.volces.com and your region to cn-beijing.​
endpoint = 'Provide your endpoint'​
region = 'Provide your region'​
vector_bucket_name = 'Provide your bucket name'​
# Obtain the account ID (usually obtainable from environment variables or configuration)​
account_id = os.getenv('TOS_ACCOUNT_ID')​
​
try:​
# Create a VectorClient object; all vector operations are handled through VectorClient.​
client = tos.VectorClient(ak, sk, endpoint, region)​
​
# Set the vector index name and the list of vector keys to be deleted.​
index_name = 'test'​
# List of vector keys to be deleted, which correspond to previously written vector data.​
keys_to_delete = [​
'vector-key-1',​
'vector-key-2', ​
'vector-key-3'​
]​
​
# Call the delete_vectors method to batch delete vector data.​
result = client.delete_vectors(​
vector_bucket_name=vector_bucket_name,​
account_id=account_id,​
index_name=index_name,​
keys=keys_to_delete​
)​
​
# Print the result information of the deletion operation.​
print('Delet vectors successfully.')​
print('Request ID: {}'.format(result.request_id))​
print('Status code: {}'.format(result.status_code))​
print('Response header: {}'.format(result.header))​
​
except tos.exceptions.TosClientError as e:​
# The operation failed and a client exception was caught. This is usually due to invalid request parameters or a network exception.​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# The operation failed and a server exception occurred. See the response for the error details.​
print('fail with server error, code: {}'.format(e.code))​
# Use the request ID for troubleshooting purposes. We strongly recommend including the request ID in the logs.​
print('error with request id: {}'.format(e.request_id))​
print('error with message: {}'.format(e.message))​
print('error with http code: {}'.format(e.status_code))​
print('error with ec: {}'.format(e.ec))​
print('error with request url: {}'.format(e.request_url))​
except Exception as e:​
print('fail with unknown error: {}'.format(e))​
​
Related documentation​
For the API documentation on DeleteVectors, see DeleteVectors.​
Last updated: 2026.02.06 18:01:46