skip to content

A RAG source page shrinks from 12 chunks to 9 and ingestion upserts by chunk ID: what is left behind, and what stops it?

level: middleimportance: must knowfreq 58%

answer

  1. upsert only touches what you send
  2. the tail the new run never names
  3. remember last run's chunk set
  4. old set minus new set
  5. an emptied page is a delete

basics

~20 s

Chunks at positions 9 to 11 stay in the index as orphans with the page's old text, because an upsert only writes the IDs it is given. Record each document's chunk IDs and delete the ones the new run did not write.

solid answer

~40 s

An upsert by chunk ID overwrites positions 0 to 8 and never mentions 9, 10 or 11, so the old last three chunks survive with stale text and keep being retrieved and quoted. Nothing in the new run refers to them, so nothing removes them. The fix is to keep per-document state from the previous run, the chunk count or the list of chunk IDs, in a small registry next to the index. After writing the new set, compute `old_ids - new_ids` and delete those. A blunter alternative is to delete everything with that `doc_id` and re-insert, which needs a delete-by-metadata call and opens a window with no chunks. The same rule covers a page edited down to nothing: zero chunks is a delete of the whole set, not a skip.

go deeper

for a junior

Recall that an upsert writes only the records you send. Name the three orphaned positions and say they are still retrieved.

for a middle

Explain the registry of previous chunk IDs or counts, the old-minus-new delete, and why the registry is updated last.

for a senior

Cover the emptied page, the crash between upsert and delete, stores without delete by metadata, and the sweep that catches what slips through.

for a principal

Make the per-document registry a required part of every ingestion path, so no connector can ship a write path that leaves orphans when documents shrink.

## What an upsert does and does not touch In a retrieval-augmented generation (RAG) index, each source document is stored as many **chunks**, each a record with an ID, an embedding vector and metadata. A common, sound design builds the chunk ID from the document ID plus the chunk's position: `docs-site:4812#0000` to `docs-site:4812#0011` for a twelve-chunk page. The nightly job then **upserts** each chunk: write the record, replacing any record with the same ID. Now the page is rewritten and shortened, and the chunker produces nine chunks. The job upserts `#0000` to `#0008`. Those nine records now hold the new text. The records `#0009`, `#0010` and `#0011` are never mentioned, so they keep the old text and the old vectors. They are **orphans**: records whose owner no longer claims them. An upsert is a per-record operation. It replaces what you send and says nothing about what you do not send. That is the whole bug. ## Why orphans hurt - **They are still retrievable.** The orphaned chunks keep their vectors, so a query about the removed section still matches them and the assistant quotes text the page no longer contains. - **They look legitimate.** Their payload says they belong to page 4812, with its title and URL, so a citation points the user at a page that does not say what the answer claims. - **They accumulate.** Each shrink leaves a few more, and nothing in the normal run ever revisits them. ## The fix: remember the previous chunk set The pipeline needs state from the last run, kept in a small **document registry** (a table keyed by document ID) beside the vector store: 1. Before writing, read the document's registry row: its previous chunk IDs (or just its previous chunk count, if IDs are positional). 2. Chunk and embed the new version; upsert its chunks. 3. Compute the difference: `stale = old_ids - new_ids`. For 12 to 9 that is positions 9, 10 and 11. 4. Delete the stale IDs. 5. Write the new chunk list (or count) to the registry, only after steps 2 to 4 succeed, so a failed run retries the cleanup next time. | Approach | Needs | Leaves | |---|---|---| | Upsert only | Nothing | Orphans whenever a document shrinks | | Upsert, then delete `old - new` | Previous chunk IDs or count | No orphans; a brief mix of old and new during the write | | Delete all chunks of the document, then insert | Delete by `doc_id` filter, or the old ID list | No orphans; a window where the page has no chunks | | Write a new generation, flip, retire the old one | Generation in the ID and a read-side check | No orphans and no visible mix | The last two rows are about **what a query sees during the replacement**, a separate design choice. Every row below the first fixes the orphan problem itself. ## When the store cannot delete by metadata Some vector stores can delete every record whose payload matches `doc_id = X`; some cannot, or do it slowly. Do not rely on it. With positional IDs and a stored previous count, the stale IDs are computable (`#0009` to `#0011`). With any other scheme, store the full ID list per document. Either way, the registry, not the store, is your record of what each document owns. ## The edge cases interviewers push on - **A page emptied to zero text.** Many pipelines skip a document that yields no chunks, which leaves all twelve old chunks in place forever. Zero chunks must mean "delete the whole set". - **A page that grows.** Twelve to fifteen leaves no orphans; the three new positions are plain inserts. - **A crash between upsert and delete.** The registry still holds the old list, so the next run computes the same difference and finishes the cleanup. This is why the registry is updated last. - **A failed delete.** Treat it like a failed write: do not advance the registry, and let a periodic orphan sweep catch anything that slips through. The store's own handling of a delete, such as how long an acknowledged delete takes to stop appearing in results, or how deleted vectors affect an approximate nearest-neighbour graph, is the vector store's subject. The pipeline's job ends at issuing the right deletes for the right IDs.

  • The vector store cannot delete by a metadata filter. How do you still remove the orphans?
    Keep the IDs yourself. With positional IDs, the registry's previous chunk count is enough: the stale IDs are the positions from the new count up to the old count minus one. With any other scheme, store the full chunk ID list per document and delete the set difference by ID.
  • Why not simply delete every chunk of the document and re-insert the new ones each night?
    It removes orphans, but between the delete and the insert the page has no chunks, so queries about it find nothing, and if embedding fails after the delete the page stays missing until a later run succeeds. It also needs a delete by document or the old ID list anyway.
  • Why update the registry only after the deletes succeed?
    The registry row is the only record of what the document owned. If it is overwritten with the new nine-chunk list before the three deletes land and the job then crashes, the next run sees nothing stale and the orphans become permanent.

Replacing a 12-volume reference set with a 9-volume new edition by putting each new volume into the slot of the old volume with the same number: volumes 10 to 12 of the old edition stay on the shelf, and readers keep taking them down until someone removes them on purpose.

  • Upsert replaces the whole document, so a shorter page cleans itself up.
  • Orphans are harmless because their content hash no longer matches.
  • If a page produces no chunks, skipping it is safe.
  • Delete-by-metadata always works, so there is no need to store chunk IDs.
  • The store's compaction will remove chunks the page no longer has.