RAG · Delete documents from a RAG workspace7 / 9
  1. 01
  2. 02
  3. 03
  4. 04
  5. 05
  6. 06
  7. 08
  8. 09

Delete documents from a RAG workspace

Example on GitHub(packages/sdk/examples/rag/rag-hyperdb/workspaces.ts)

The previous lessons added documents to a workspace. This one removes them.

ragDeleteEmbeddings({ workspace, ids }) takes the workspace name and a list of ids to remove. The ids come from the processed array returned by ragIngest. The editor prefills the ingest call and the id collection; the new piece is the search + delete + re-search flow.

Without a baseline, the second search is just a number. Counting search results before the delete would look as follows:

const before = await ragSearch({ modelId, workspace, query: "machine learning", topK: 5 });
console.log(`▸ Before delete: ${before.length} matches`);

Now the delete. ragDeleteEmbeddings({ workspace, ids }) removes each id in the list, and the call is idempotent: passing an id that doesn't exist is a no-op. Here's how that looks in code:

await ragDeleteEmbeddings({ workspace, ids: [ids[0]!] });
console.log(`▸ Deleted embedding ${ids[0]}`);

const after = await ragSearch({ modelId, workspace, query: "machine learning", topK: 5 });
console.log(`▸ After delete: ${after.length} matches`);

In the call, ids is string[]. Pass one entry to delete a single document, or several to delete them in one call. After the delete, the workspace's index is updated. ragSearch returns one fewer result on the next call. If you're running repeated delete + ingest cycles, follow up with ragReindex after a few hundred writes to keep scores tight.

Note: ragDeleteEmbeddings removes the documents but doesn't shrink the on-disk index until you reindex. The next ragSearch skips the deleted ids, but the index file still occupies the original size until ragReindex is called.

Put it to the test

  1. The editor prefills the ragIngest + ids collection. Add a search before the delete and log the count.
  2. Call ragDeleteEmbeddings({ workspace, ids: [firstId] }), search again, and log the new count.
index.ts

$ Run your code to see results

$