How to delete documents from Chroma
Quick answer
To delete documents from
Chroma, use the delete method on your collection object, specifying document IDs or filters. This removes the vectors and metadata associated with those documents from the vector store.PREREQUISITES
Python 3.8+pip install chromadbBasic familiarity with vector databases and Python
Setup
Install the chromadb Python package and import it. Initialize a client and create or connect to a collection where your documents are stored.
import chromadb
client = chromadb.Client()
collection = client.get_or_create_collection(name="example_collection") Step by step
Use the delete method on the collection to remove documents by their IDs or by a metadata filter.
import chromadb
client = chromadb.Client()
collection = client.get_or_create_collection(name="example_collection")
# Add sample documents
collection.add(
documents=["Document 1", "Document 2", "Document 3"],
ids=["doc1", "doc2", "doc3"]
)
print("Before deletion:", collection.get(ids=["doc1", "doc2", "doc3"]))
# Delete document with id 'doc2'
collection.delete(ids=["doc2"])
print("After deletion:", collection.get(ids=["doc1", "doc2", "doc3"])) output
Before deletion: {'ids': ['doc1', 'doc2', 'doc3'], 'documents': ['Document 1', 'Document 2', 'Document 3'], ...}
After deletion: {'ids': ['doc1', 'doc3'], 'documents': ['Document 1', 'Document 3'], ...} Common variations
You can delete documents by metadata filters instead of IDs, for example deleting all documents where a metadata field matches a value.
collection.delete(filter={"category": "obsolete"}) Troubleshooting
If deletion does not seem to work, verify that the document IDs or filter keys exactly match those in the collection. Also, ensure you are calling collection.persist() if using a persistent Chroma setup.
Key Takeaways
- Use
collection.delete(ids=[...])to remove specific documents by ID. - You can also delete documents by metadata filters with
collection.delete(filter={...}). - Always verify document IDs and metadata keys match exactly to avoid failed deletions.
- Call
collection.persist()if using persistent storage to save changes. - Deleting documents removes both vectors and associated metadata from Chroma.