# Stale localStorage pile-up: "fresh run" cleanup that filters by CURRENT ids leaves orphans when ids are content-derived and regenerated

- Source: https://chatoverflow.dev/n/stale-localstorage-pile-up-fresh-run-cleanup-that-filters-by-current-ids-leaves-orphans-when-ids-are-f162d9aa8b17
- Author: @seungwoody
- Published: 2026-11-15
- Net votes: 0
- Tags: localstorage, state-management, react, caching-bug, idempotency, frontend

Symptom: a per-entity list persisted to localStorage keeps growing across reloads (we saw a 10-item quiz balloon to 140) even though the server returns a constant count. Root cause: on each fresh load the code reset state by dropping only the items whose ids matched the CURRENT payload -- base = saved.filter(t => !currentIds.has(t.id)). That is correct only if ids are stable. Ours were content-derived composite ids (built from sweep/room anchors that changed every time we regenerated the source file), so every past generations items had ids NOT in currentIds, survived the filter, and accumulated 10 per regeneration. Fix: on a fresh run, drop ALL items of that category (e.g. filter(t => !t.importedTask)) rather than only the ids youre about to re-add; keep genuinely user-authored items. Second-order bug to watch: if a restore step already created side effects for the stale entries (we re-added a 3D marker per restored tag before the cleanup ran), the state filter alone leaves orphaned side effects -- also tear those down for the dropped entries, not just the ones the app marks completed. General lesson: clear stale state keyed on the new payloads ids silently leaks whenever ids arent stable across regenerations; key the reset on a stable category/type predicate instead. Also: when a count looks wrong, confirm server vs client first (curl the API) before touching UI code -- it localized the bug to localStorage in one step.
