🗑️Drop
This guide explains how to drop (delete) existing indexes from the enVector server. There are two main approaches: using the Index object's drop() method or using the pyenvector API directly.
Overall Usage
The process of dropping indexes involves these main steps:
Connect to Server - Ensure connection to the enVector server
Verify Index Existence - Check if the index exists before attempting to drop it
Choose Drop Method - Select between Index object method or direct API call
Execute Drop Operation - Remove the index from the server
Verify Deletion - Confirm the index has been successfully removed
Detailed Usage Using Arguments
1. Connect to the Server
First, ensure you have a connection to the enVector server:
import pyenvector as ev
# Initialize connection to the server
HOST_ADDRESS = "your.hostname:50050"
ev.init_connect(address=HOST_ADDRESS)
print("Connected to enVector server successfully")2. Verify Index Existence
Before dropping an index, verify it exists on the server:
# Get list of all indexes
index_list = ev.get_index_list()
if "my_index" in index_list:
print("Index 'my_index' exists on the server")
else:
print("Index 'my_index' not found on the server")3. Method 1: Drop Using Index Object
You can drop an index by first creating an Index object and then calling its drop() method. Note: This method requires you to initialize the index configuration, as it is needed to create the Index object itself.
from ev.index import Index
import pyenvector as ev
"""Drop an index using the Index object's drop method"""
try:
# Initialize Index Config
ev.init_index_config(key_path="./keys", key_id="your-key-id")
# Create Index object for the existing index
index = Index(index_name)
print(f"Index object created for '{index_name}'")
print(f"Index dimension: {index.dim}")
print(f"Number of entities: {index.num_entities}")
# Drop the index
print(f"Dropping index '{index_name}'...")
index.drop()
print(f"Index '{index_name}' dropped successfully")
except Exception as e:
print(f"Failed to drop index '{index_name}': {e}")Important Notes:
The
drop()method removes the index from the serverAfter dropping, the Index object becomes invalid
The method returns the Index object itself for chaining operations
4. Method 2: Drop Using pyenvector API Directly
For a more direct method that avoids initializing the index configuration, call the pyenvector API. This approach bypasses the need for an Index object and only requires the name of the index you wish to remove.
"""Drop an index using the pyenvector API directly"""
try:
# Check if index exists
index_list = ev.get_index_list()
if index_name not in index_list:
print(f"Index '{index_name}' not found on server")
return False
# Drop the index using pyenvector API
print(f"Dropping index '{index_name}' using API...")
ev.drop_index(index_name)
print(f"Index '{index_name}' dropped successfully via API")
except Exception as e:
print(f"Failed to drop index '{index_name}' via API: {e}")6. Complete Working Example with Safe Deletion
Here's a complete script that demonstrates both drop methods and includes safe deletion functionality:
import pyenvector as ev
from pyenvector.index import Index
def main():
# 1. Connect to server
HOST_ADDRESS = "localhost:50050"
ev.init(address=HOST_ADDRESS, key_path="./keys", key_id="test-key")
print("Connected to enVector server")
# 2. List current indexes
index_list = ev.get_index_list()
if not index_list:
print("No indexes found on the server")
return
print(f"Current indexes: {index_list}")
# 3. Demonstrate both drop methods with safe deletion
for index_name in index_list:
print(f"\n{'='*50}")
print(f"Processing Index: {index_name}")
print(f"{'='*50}")
try:
# Method 1: Using Index object
print("Method 1: Using Index object")
index = Index(index_name)
print(f"Index object created: {index}")
# Method 2: Using pyenvector API
print("\nMethod 2: Using pyenvector API")
index_info = ev.get_index_info(index_name)
print(f"Index info: {index_info}")
# Demonstrate safe deletion (with confirmation)
print(f"\nSafe deletion demonstration:")
print("(This is a demonstration - no actual deletion will occur)")
# Show what would happen in a real scenario
print(f"Would show index details before deletion:")
print(f" Dimension: {index_info['dim']}")
print(f" Row Count: {index_info['row_count']}")
print(f" Created Time: {index_info['created_time']}")
except Exception as e:
print(f"Error processing index '{index_name}': {e}")
print(f"\n{'='*50}")
print("Index drop demonstration completed")
print("Note: No indexes were actually dropped in this demonstration")
if __name__ == "__main__":
main()7. Error Handling
Implement comprehensive error handling for production use:
def robust_drop_index(index_name):
"""Robust index deletion with comprehensive error handling"""
try:
# Check connection
if not ev.is_connected():
print("Not connected to server. Please connect first.")
return False
# Check if index exists
index_list = ev.get_index_list()
if index_name not in index_list:
print(f"Index '{index_name}' not found on server")
return False
# Attempt to drop
ev.drop_index(index_name)
# Verify deletion
updated_list = ev.get_index_list()
if index_name not in updated_list:
print(f"Index '{index_name}' successfully dropped")
return True
else:
print(f"Warning: Index '{index_name}' may still exist")
return False
except ValueError as e:
if "Indexer not connected" in str(e):
print("Server connection error. Please check your connection.")
elif "Key ID not found" in str(e):
print(f"Key not found for index '{index_name}'. Please register the key first.")
else:
print(f"Configuration error: {e}")
return False
except Exception as e:
print(f"Unexpected error dropping index '{index_name}': {e}")
return False8. Best Practices
Always Verify Existence: Check if an index exists before attempting to drop it
Backup Important Data: Ensure you have backups before dropping production indexes
Use Confirmation: Implement user confirmation for critical operations
Verify Deletion: Check that the index was actually removed after the drop operation
Error Handling: Implement comprehensive error handling for network and server issues
Important Note: When you want to delete a registered key from the server, you must first drop all indexes that use that key. The server will not allow you to delete a key that is still associated with existing indexes. Always drop dependent indexes before attempting to delete their associated keys.
9. Troubleshooting
Common Issues and Solutions:
"Indexer not connected": Reinitialize the SDK with
ev.init()"Index not found": Verify the index name exists using
ev.get_index_list()Drop operation fails: Check server logs and ensure proper permissions
This guide provides comprehensive coverage of dropping indexes from the enVector server using both the Index object method and the direct pyenvector API approach.
Last updated

