Faiss Index.remove_ids()

Created
Created
2023 Oct 31 13:56
Creator
Creator
Seonglae ChoSeonglae Cho
Editor
Edited
Edited
2025 Oct 28 11:11
Refs
Refs

Requirements

  1. ID mapping support: The base index must be wrapped with IDMap or IDMap2 to enable ID-based operations
  1. Valid ID format: IDs must be provided as a numpy array of integers
  1. Existing IDs: Only IDs that exist in the index can be removed; attempting to remove non-existent IDs may result in warnings or errors

Implementation Example

import faiss import numpy as np # Create base index and wrap with IDMap dimension = 128 base_index = faiss.IndexFlatL2(dimension) index = faiss.IndexIDMap(base_index) # Add vectors with specific IDs vectors = np.random.random((100, dimension)).astype('float32') ids = np.arange(100) index.add_with_ids(vectors, ids) # Remove specific vectors by their IDs ids_to_remove = np.array([5, 10, 15, 20]) index.remove_ids(ids_to_remove) print(f"Vectors remaining: {index.ntotal}")
 
 
 
 
 
 

Recommendations