All posts

RAG knowledge base freshness: how to keep a RAG index in sync when documents change or get deleted

A RAG index goes stale the day after launch. How to detect changed and deleted documents, sync incrementally with content hashes, and stop retired policies from being quoted as current.

CodeKrypt 6 min read

A RAG system is usually demoed on a snapshot of documents that is correct the day it is indexed. Then the refund policy changes, a price list is replaced, an employee handbook section is withdrawn, and the assistant keeps answering from the old text with full confidence. RAG knowledge base freshness is the problem of making sure the index tracks the source of truth, including the documents that disappear.

This post covers how to detect change, sync incrementally instead of re-embedding everything, handle deletions, and make staleness visible when it does slip through.

Why does a RAG index go stale?

The index is a copy. It is built from documents at one moment and then diverges, in three ways:

The deletion case is the dangerous one. A stale edit returns an outdated answer; a stale deletion can return content you were legally or contractually required to remove. If personal data is involved, erasure has to reach the vector store too, as discussed in our DPDP Act and AI compliance guide.

How do you detect that a document changed?

Pick the cheapest signal your source reliably offers:

SignalHow it worksWeakness
Source webhook or change feedSource pushes an event on create, update, deleteEvents can be dropped; needs a safety-net scan
updated_at / ETag / versionPoll and compare to the stored valueSome systems bump it without real content change, or not at all
Content hashHash the extracted text and compareRequires fetching and parsing every document on each scan
Full re-index on a scheduleRebuild everything nightly or weeklyWasteful, and leaves a gap between runs

A robust setup combines two: events or timestamps for speed, plus a periodic hash-based reconciliation scan that catches whatever the events missed. Treat the scan as the source of correctness and the events as an optimisation.

What does an incremental sync look like?

Incremental RAG sync: list source documents, compare hashes with the index manifest, re-embed changed ones, delete missing ones.Source listingDiff vs manifestNew / changed: re-chunk, embed, swapMissing from source: delete chunks

The core is a manifest: one row per source document holding its stable ID, content hash, last-indexed time, and the IDs of the chunks it produced. Each sync run then does the following:

  1. List the documents currently in the source (IDs plus cheap change signals).
  2. For each ID, compare to the manifest. Unknown ID means new; different hash means changed.
  3. For new and changed documents, extract text, chunk, embed, and write the new chunks.
  4. Replace, don't append. Delete the document's old chunks in the same operation (or transaction) that inserts the new ones. Appending without deleting leaves old and new versions both retrievable.
  5. For IDs in the manifest but not in the source listing, delete their chunks and the manifest row.
  6. Record the run: counts of added, updated, deleted, failed.

Because only changed documents are embedded, the cost of a sync scales with the amount of change, not the size of the corpus. Chunk boundaries matter here: if your chunking strategy makes a small edit shift every chunk in a document, re-embedding that whole document is the unit of work, which is fine and simple. Chunk-level diffing is rarely worth the complexity.

How do you make a replacement safe?

A document should never be half-updated in the index. Two patterns work:

Either way, make the job idempotent: running the same sync twice must leave the same result, so a crashed run can simply be restarted.

What happens to the index after many deletes?

Deleting is not free in an approximate index. The pgvector README notes that vacuuming can take a while for HNSW indexes, suggesting a reindex first, and that queries on an HNSW index can return fewer results than expected "due to dead tuples or filtering conditions in the query." It also explains that with approximate indexes, filtering is applied after the index is scanned, so a tight filter on top of an index full of stale rows can starve the result set. If you filter on "current version only", test retrieval at your real filter selectivity, and schedule vacuum and reindex maintenance. Other vector databases have their own compaction behaviour; read the docs for yours. For the wider storage decision see pgvector vs a dedicated vector database.

How do you stop stale answers reaching users?

Sync reduces staleness; it cannot eliminate it, because there is always a gap between a source change and the next sync. Add defences at answer time:

What should you test?

Add freshness cases to your evaluation set, as described in LLM evals in CI:

What to do next

Answer three questions for each source you index: how does a change reach the index, how does a deletion reach it, and how would you know if either failed? If you cannot answer the second one, start there, because deletions are what simple "add new files" pipelines miss. Then write a freshness target per source so the engineering effort matches the cost of a stale answer.

If you are building a retrieval layer that has to stay correct as content changes, see how we approach AI SaaS development and get in touch.

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

How often should a RAG index be refreshed?
It depends on how costly a stale answer is, not on a standard interval. Pricing, policy and compliance content usually needs change-driven updates (webhooks or change feeds); slow-moving documentation can be re-scanned nightly. Decide per source and write the freshness target down.
Do I have to re-embed everything when one document changes?
No. Store a content hash per document, re-chunk only documents whose hash changed, and replace that document's chunks. A full re-embed is only needed when the embedding model or chunking strategy changes.
How do deleted documents get removed from a RAG index?
Deletions are not visible if you only look for new or changed files. Either subscribe to delete events from the source, or compare the set of document IDs in the source with the set in the index on each sync and remove the difference.
Can the assistant tell users how old its source is?
Yes, if you store a last-updated date as chunk metadata and include it in the retrieved context and in citations. This lets the model, and the user, see when an answer rests on old material.

Related reading

Chat on WhatsApp