🛠️ Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
CodeLens reference counts come from a background indexer tied to the workspace’s own source database, and that index updates incrementally from whatever edits it has actually processed so far. After a large cross-project rename or refactor, Visual Studio doesn’t always force a full re-index on every save — it relies on incremental deltas that can lag behind a change spanning many files, so the margin keeps showing whatever count the indexer last finished computing instead of the real one.
🔎 The Problem
Before renaming callers in two other projects: CalculateTotal(order) 3 references After renaming all three call sites to CalculateOrderTotal(order): CalculateOrderTotal(order) 3 references <- still shows the pre-rename count (actual call sites updated: 3, but CodeLens index hasn't caught up)
✅ Fix: Force a Full Re-Index Instead of Trusting the Incremental One
- Check
ALLOWED DISRUPTIONS-style staleness the simple way first: close and reopen the solution, which forces CodeLens to rebuild its index from scratch rather than continuing from an incremental delta that a big rename can outrun. - For a cross-project rename specifically, run a full Rebuild Solution (not just Build) right after, since that’s the trigger that reliably requeues every affected file for the background indexer instead of relying on deltas.
- If counts still look wrong afterward, close Visual Studio and delete the solution’s CodeLens cache under the hidden
.vsfolder so the next launch builds it fresh rather than patching an index that’s already confused.
🚨 Why This Is Dangerous
- Trusting a stale “0 references” count during a cleanup pass can lead you to delete a method or type that’s still called from another project the index hasn’t caught up with yet, and the break only shows up once that other project compiles on its own.
CodeLens tells you what its index believes is true right now, not what’s true in your code — and a background indexer can fall behind a big refactor for longer than a glance at the margin would ever suggest.
