[Python] Trouble deleting ChromaDB documents

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I can't seem to delete documents from my Chroma vector database. I would appreciate any insight as to why this example does not work, and what modifications can/should be made to get it functioning correctly.

    import dotenv
    import os
    import chromadb
    from chromadb.config import Settings
    from chromadb.utils import embedding_functions

    dotenv.load_dotenv()

    client = chromadb.Client(
    Settings(chroma_db_impl="duckdb+parquet", persist_directory="db/chroma")
    )

    embedding = embedding_functions.OpenAIEmbeddingFunction(
    api_key=os.getenv("OPENAI_API_KEY"),
    model_name="text-embedding-ada-002",
    )

    collection = client.get_or_create_collection(name="test", embedding_function=embedding)

    from llama_index import SimpleDirectoryReader

    documents = SimpleDirectoryReader(
    input_dir="./sampledir",
    recursive=True,
    exclude_hidden=False,
    filename_as_id=True,
    ).load_data()

    collection.add(
    documents=[doc.get_text() for doc in documents],
    ids=[doc.doc_id for doc in documents],
    )

    print(collection.count()) # PRINTS n

    doc_ids = collection.get()["ids"]
    collection.delete(ids=doc_ids)

    print(collection.count()) # SHOULD BE ZERO, BUT PRINTS n

    Continue reading...

Compartilhe esta Página