---
title: "Keep search indexes in step with their · Agent Wranglers"
url: https://agentwranglers.se/playbook/search/index-contents-and-sync/search-index-sync
id: SRH-7
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 969
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Keep search indexes in step with their source

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Index contents and sync](https://agentwranglers.se/playbook/search/index-contents-and-sync.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: SRH-7
- Level: Recommended
- Chapter: [Index contents and sync](https://agentwranglers.se/playbook/search/index-contents-and-sync.md)
- Rule: Update or remove a record's index entries within a set time after the source changes, such as 15 minutes, through a reliable queue. Rebuild the whole index on a schedule to repair drift.
- Check: Delete a test record at the source. Within the set time, a search for its unique phrase returns nothing. A nightly job compares record IDs in the source and the index and alerts on any difference.
- How: The index is a copy, as [Name the source of truth](https://agentwranglers.se/playbook/data/caches-and-copies/source-of-truth.md) says. Write each index update to an outbox in the same transaction as the change, as [Send side effects through an outbox](https://agentwranglers.se/playbook/events/publishing-events/outbox.md) describes. A worker then applies it. Index on these change events, not only in a nightly job. Delete entries by record ID when a record is deleted or unpublished. Store the source's last-modified time with each entry, and show it with AI answers. Run a full rebuild or a compare on a schedule to catch missed events.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### SQL example

```sql
SELECT c.document_id FROM chunks c LEFT JOIN documents d ON d.id = c.document_id
WHERE d.id IS NULL OR (c.source_updated_at < d.updated_at AND d.updated_at < now() - interval '15 minutes');
-- PASS when no rows are returned
```

### TypeScript example

```ts
documents.on('deleted', async ({ id }) => vectorIndex.deleteWhere({ documentId: id }));
```

### Python example

```python
@documents.on("deleted")
def remove_from_index(event: DocumentDeleted) -> None:
    vector_index.delete_where(document_id=event.id)
```

## Sources

- [Microsoft Azure Architecture Center on the Transactional Outbox pattern](https://learn.microsoft.com/en-us/azure/architecture/databases/guide/transactional-out-box-cosmos)

## Related rules

- DAT-44 [Name the source of truth](https://agentwranglers.se/playbook/data/caches-and-copies/source-of-truth.md) in Data and databases
- STK-33 [Use PostgreSQL search first and OpenSearch when it falls short](https://agentwranglers.se/playbook/stack/databases-and-storage/full-text-search.md) in Software choices
- EVT-6 [Send side effects through an outbox](https://agentwranglers.se/playbook/events/publishing-events/outbox.md) in Integration and events
- SRH-8 [Remove erased records from every index](https://agentwranglers.se/playbook/search/index-contents-and-sync/erase-from-indexes.md) in Search

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/search/index-contents-and-sync/search-index-sync">Agent Wranglers search playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Index only the fields people search](https://agentwranglers.se/playbook/search/index-contents-and-sync/search-index-fields.md)
- Next: [Remove erased records from every index](https://agentwranglers.se/playbook/search/index-contents-and-sync/erase-from-indexes.md)
- [Index contents and sync](https://agentwranglers.se/playbook/search/index-contents-and-sync.md)
