Your RAG index still remembers the documents the client deleted
Give every chunk a stable source-document ID and keep a record of what was indexed, and edits and deletions sync without re-embedding everything from scratch.
In brief
- Every chunk must carry the stable ID of its source document. Without it, nothing can be deleted or updated correctly.
- Incremental only cleans up documents that appear in the current run. Removing documents the client deleted takes full mode, and the loader must return the complete dataset.
- On Qdrant, upsert replaces the whole point. On Pinecone, deletions are not visible immediately. Design the verification step accordingly.
A typical Monday morning: the client’s operations team reports that the old claims procedure was pulled from the document repository on Friday, yet the chatbot is still quoting it to staff. The new version has been in the index for a long time. The old one was simply never deleted.
This is one of the most common failures when RAG runs on real data. In a demo, the document set stands still. In production, the client edits, renames and removes documents every week. Sooner or later, the question of how to keep the index in step with the source lands on the FDE’s desk.
The simplest fix is to drop the whole collection and re-embed from scratch. That works while the corpus is small. But it hides the real problem: your system does not know which chunk came from which document. Once the corpus grows too large to rebuild every week, that debt comes due.
Every chunk must know where it came from
Every sync mechanism rests on one principle: each chunk carries the ID of its source document, and something keeps a record of what has been indexed. In LangChain, that something is the record manager, a set of timestamped records showing which documents have been updated. The source_id_key parameter links each chunk to its source document.
The ID has to stay stable across runs. LlamaIndex is explicit about this: refresh only works automatically when documents have a fixed id_. With SimpleDirectoryReader, the filename_as_id flag uses the file name as the ID. Generate random IDs at load time and the system treats every run as an entirely new set of documents.
Once the source ID sits in the metadata, the vector database itself can delete by document. Qdrant deletes points with a payload filter instead of a list of IDs. Pinecone lets you delete records in a namespace by metadata value. A single field such as source is enough to remove every chunk of a document with one command.
The wrong cleanup mode loses data
LangChain’s Indexing API offers several cleanup modes. The three below reflect three different answers to the question of which chunks are stale.
| Mode | What it cleans up, and when to use it | Caveats |
|---|---|---|
incremental |
Old chunks for the source IDs that appear in this run | If chunks of one document are spread across several batches, the API does redundant work, so keep them together |
full |
Every document the loader does not return in this run | The loader must return the entire dataset, not part of it |
scoped_full |
For when the loader cannot return all the data at once, or batch size is hard to choose | Available from langchain-core 0.3.25 |
The crucial detail is in the middle column. Incremental only cleans up what it “sees”. A document the client deleted from the source does not appear in the run, so incremental never touches it. That is why the old claims procedure was still in the index.
Full mode does remove deleted documents, but it is also the most dangerous. The LangChain documentation warns that in full mode the loader must return the entire dataset. If it returns only part of it, say because the client’s API paginates and you forgot to fetch the next page, full mode treats everything else as garbage and deletes it.
A worked example from start to finish
The numbers that follow are hypothetical, for illustration. Take a client with 1,200 policy documents. This week they edit 30 files, add 10 and remove 5. You want the index to reflect exactly those three changes, and to leave the 1,165 unchanged documents alone.
import json
import pathlib
from langchain.indexes import SQLRecordManager, index
record_manager = SQLRecordManager(
namespace="acme_policies/qdrant",
db_url="sqlite:///record_manager.db",
)
record_manager.create_schema()
docs = load_all_policies() # your loader; metadata["source"] = the client's stable document ID
# Source-document count from the previous run, kept in a small file
state = pathlib.Path("last_run.json")
last_count = json.loads(state.read_text())["sources"] if state.exists() else 0
current_sources = {d.metadata["source"] for d in docs}
if len(current_sources) < 0.9 * last_count:
raise RuntimeError("Loader returned too few documents; stopping before cleanup")
result = index(
docs,
record_manager,
vector_store,
cleanup="full",
source_id_key="source",
)
print(result)
state.write_text(json.dumps({"sources": len(current_sources)}))
Walk through each group of changes. For the 30 edited files, the record manager sees different content, so it writes new chunks and cleans up the old ones. For the 10 new files, chunks are added. The 5 removed files are not in docs, so full mode deletes their chunks. Everything else matches the existing records and is skipped without being re-embedded.
Now run the same code in the same scenario, but with a broken loader that returns only the 40 changed files. Without the 0.9 check, full mode would treat the 1,170 indexed documents the loader did not return as removed, and delete them.
The 90% figure comes from no documentation. It is a safety catch you set yourself, and it should be tuned to each client’s actual rate of change. Note that the count is written only after index() finishes, so a failed run does not corrupt next week’s baseline.
If you ever switch to incremental, keep the chunks of each document together in docs. LangChain warns that when the chunks of one source ID fall into different batches, the API does redundant work.
Without LangChain
LlamaIndex has refresh(). It updates only documents with the same id_ whose content has changed, re-embeds only those, and in the same call inserts documents not yet in the index. Refresh does not handle deletion. Removed documents must be deleted separately by ID, and the delete call removes every node of that document.
There is a small trap here: delete_from_docstore defaults to False, because nodes may be shared by several indexes using one docstore. If your project does not share a docstore, turn the flag on yourself, or the docstore will keep growing over time.
If you write directly to Qdrant, understand exactly what upsert does. When you upsert a point whose ID already exists, Qdrant deletes the old point and reinserts it with only the vectors you passed in.
Say the collection holds both dense and sparse vectors and you only want to re-embed the dense part. Use the update vectors operation instead, which leaves any vectors you do not mention untouched.
With Pinecone, the issue is timing. Pinecone is eventually consistent, so for a short while after a delete or update, queries may still return old chunks. A sync job that verifies its results immediately after writing will report false failures. Wait a little before running the check.
The mistakes that make chatbots quote deleted documents
The most common mistake is unstable IDs. If file names include dates, or the client renames files often, filename_as_id turns every rename into a new document, and the old version stays in the index.
During discovery, ask the client whether the source system has a fixed document ID that does not change with the name, and use it where it exists.
The second mistake is running incremental and believing everything is clean. The third is the reverse: running full with a loader nobody has ever checked for completeness. Both run smoothly until the week the client deletes their first document, or the week their API fails halfway through.
This skill also deserves its own line on a CV. Rather than “built a RAG system”, describe how you kept the index in step with a source that changed weekly, and how you prevented accidental deletions. When a job description mentions an ingestion pipeline or data freshness, that is the place to tell this story in an interview.
A RAG system is judged at the demo. The client decides whether to trust it in the first week the chatbot answers with a document they had already removed.
Was this article useful?
Thanks for the feedback!