DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

Sync a Whoosh Index with Folder Changes in Python

Use stored unique file paths and a change marker to add, replace, or delete only the files that changed, then commit the sync as one batch.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To keep a Whoosh index current without rebuilding it, reconcile the paths already indexed with the files currently in a folder: delete missing paths, replace changed files, add new ones, and skip unchanged files. Store each file’s path as a unique indexed field and keep a change marker such as its modification time (mtime). Run the reconciliation through one writer and commit after the scan.

Set up the schema around file identity

Use the file path as the document’s identity. It must be an indexed field marked unique so Whoosh can find the existing document when a file changes; store it too, so the sync can compare indexed paths with the folder. Store the change marker alongside it. The official incremental-indexing example uses mtime for that marker. See Whoosh’s “How to index documents” example.

As an Amazon Associate I earn from qualifying purchases.

A simplified schema pattern is:

from whoosh.fields import ID, Schema, TEXT, STORED

schema = Schema(
    path=ID(unique=True, stored=True),
    mtime=STORED,
    content=TEXT,
)

Adapt the content and metadata fields to the application. The important properties for synchronization are that path is indexed, unique, and stored, and that the stored marker can be compared with the file on disk.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reconcile the index with the folder

Treat synchronization as comparing two sets: paths recorded in the index and paths found in the current filesystem scan. The official example uses mtime for simplicity. Its logic is:

  1. Read indexed documents. Collect each stored path and its recorded marker.
  2. Remove missing files. If a previously indexed path no longer exists, delete documents matching that indexed path.
  3. Mark changed files. If a path still exists but its recorded mtime is older than the current mtime, queue it for replacement.
  4. Find additions. Walk the folder. Add paths not in the indexed set; re-index paths marked changed; leave unchanged paths alone.
  5. Commit once the scan is complete. Keep the batch within one writer lifetime rather than committing once per file.

Use the same path representation when indexing and scanning—for example, consistent relative or absolute paths—so a file does not appear to be a new identity merely because its path was formatted differently.

Choose how to replace changed documents

Use update_document for straightforward individual replacements

With a unique indexed path field, a call such as writer.update_document(path=path, content=content, mtime=mtime) removes committed documents with the matching unique value and adds the replacement. If no committed document matches, it behaves as an add. This is convenient for one-off changes. The API details are in Whoosh’s writing API documentation.

There is an important edge case: update_document replaces a matching committed document, not an earlier uncommitted document in the same writer. Updating the same path multiple times before committing can therefore leave duplicates. If a scan or queue may produce repeated changes for one path, deduplicate the work before writing or use a batch delete-and-add strategy.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use batched delete-and-add for many changes

For larger batches, the documentation notes that deleting changed documents and adding their replacements can be faster than repeatedly calling update_document. Delete by the indexed path term, then add one replacement for each changed path. This approach also makes it easier to ensure that a path is handled once in the batch. Whoosh does not enforce uniqueness when documents are added with add_document, so the application must avoid adding duplicate paths.

Manage deletions and writer lifetime

Delete a missing file using its indexed identifier, then commit the writer with the rest of the sync. A deletion in Whoosh’s filedb backend is logical: it marks the document deleted but does not immediately reclaim its stored contents or all related statistics. Segment merging eventually removes deleted material. Forced optimization can rewrite index information and may be expensive, so it is not a substitute for routine synchronization.

Opening a writer acquires the index’s write lock; only one thread or process can hold a writer at a time. A competing writer may raise LockError. Keep the writer open only for the bounded batch, and make sure it is closed by committing or cancelling. A context manager commits on normal exit and cancels if an exception occurs:

with ix.writer() as writer:
    # delete missing paths; add or replace changed and new files
    ...

If using an explicit writer flow rather than a context manager, cancel it after an error and commit it when the reconciliation succeeds. The writer and lock behavior is described in the Whoosh threading documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Refresh readers after committing

Committing publishes a new index generation, but readers already open continue to see the previous version. Open a new reader or searcher when the application needs results from the latest commit; existing readers do not switch automatically. Whoosh describes this behavior in its indexing documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a change marker that fits the filesystem

mtime: simple, but not a universal change detector

The official folder-sync example compares stored and current mtimes because it is straightforward and avoids rereading unchanged file contents. It does not guarantee detection of every content change across all filesystems, timestamp resolutions, or workflows. A file can change without producing a reliably newer marker under the conditions an application cares about.

Content hashes or application versions: stronger signals with a cost

If mtime is not reliable enough, store a content digest or an application-owned version marker instead. A digest requires reading and hashing content to detect changes, which adds I/O and CPU work; a trustworthy application version can avoid that cost when the source system supplies one. The right choice depends on the required detection reliability and the cost of computing the marker—Whoosh’s example does not quantify those trade-offs across environments.

Check which Whoosh distribution you are using

The cited API documentation is for Whoosh 2.7.4. The original Whoosh package on PyPI lists version 2.7.4 as uploaded on April 4, 2016. Whoosh-Reloaded is a separate continuation and lists 2.7.5 as newer than 2.7.4. A separate project describes a 2026 continuation distributed as whoosh3. These are distinct project and distribution contexts; confirm the installed package and its current API documentation before relying on compatibility or installation advice.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.