Updating and Deleting Vectors
Keep the index in sync with the source: upsert on document change, delete on removal, and handle re-embeddings.
Updating and Deleting Vectors is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Index Is Not Frozen
Real documents change. The index must keep up:
- New docs added
- Existing docs edited
- Old docs deleted or archived
Upsert (Insert or Update)
Most vector DBs support upsert — write the vector with a given ID, replacing the old one if it exists:
index.upsert([
('doc-42', new_vector, {'source': 'a.pdf', 'updated_at': now})
])
# If doc-42 already exists, it is overwritten.Deterministic IDs
If you use content-hash IDs, "update" happens automatically — same content = same ID = same slot.
If you use UUIDs, you must track which UUID belongs to which source doc.
Detecting Changes
To diff a corpus, store a hash of every chunk:
old_chunks = load_existing_chunks(source)
new_chunks = chunk_doc(source)
old_hashes = {c.id for c in old_chunks}
new_hashes = {c.id for c in new_chunks}
to_delete = old_hashes - new_hashes
to_add = new_hashes - old_hashes
# Skip unchanged — saves re-embeddingDelete by Filter
Most DBs support deletion by filter, useful for "delete everything from this source":
index.delete(filter={'source': '/old/file.pdf'})Delete by ID
index.delete(ids=['doc-1', 'doc-2', 'doc-3'])Soft Delete
Sometimes you want to "hide" without removing — set a deleted_at flag and filter it out at query time:
index.upsert([('doc-42', vec, {'deleted_at': now})])
# At query time:
results = index.query(vector=query_vec, top_k=5, filter={'deleted_at': {'$exists': False}})Compaction
Frequent deletes leave the index sparse. Periodically rebuild for performance — most DBs do this automatically; check your provider's docs.
Re-Embedding on Model Change
If you upgrade the embedding model, every existing vector becomes incompatible. Strategies:
- Dual-write to old and new collections during migration
- Switch read traffic to new collection when complete
- Drop the old collection
Incremental Pipeline
A robust ingestion pipeline:
def sync_doc(source_path):
new_chunks = chunk_and_hash(load(source_path))
existing_ids = index.fetch_ids_by_source(source_path)
new_ids = {c.id for c in new_chunks}
to_add = [c for c in new_chunks if c.id not in existing_ids]
to_remove = existing_ids - new_ids
if to_remove:
index.delete(ids=list(to_remove))
if to_add:
index.upsert([(c.id, embed(c.text), c.metadata) for c in to_add])Auditing Drift
Periodically verify the index matches the source of truth. Count rows per source in both your DB and the vector index; alert on mismatch.
Webhook-Driven Updates
For doc management systems (Notion, Confluence, GDrive), use webhooks to trigger incremental sync on change — much cheaper than re-crawling everything.
Tombstones for Distributed Deletes
In distributed indexes, "delete" often writes a tombstone marker. Until compaction runs, queries may still see the tombstone — keep your filters in place.
Idempotent Updates
What is the easiest way to make ingestion safe to re-run?
Recap
Plan for updates from day one: hash-based IDs, diff-based ingestion, and a path to swap embedding models without downtime.
Frequently asked questions
Is the “Updating and Deleting Vectors” lesson free?
Yes — the full text of “Updating and Deleting Vectors” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Updating and Deleting Vectors”?
Keep the index in sync with the source: upsert on document change, delete on removal, and handle re-embeddings. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Updating and Deleting Vectors” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.