# MongoServerError: PlanCache invalidated entry for query shape but could not re-plan: internal error

- **ID:** `mongodb/plan-cache-invalidation-failed`
- **Domain:** mongodb
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 82%

## Root Cause

The plan cache entry for a query shape was invalidated due to index changes or data distribution shifts, but the query planner failed to generate a new plan due to corrupted cache metadata or missing index.

## Workarounds

1. **Run db.collection.getPlanCache().clear() followed by db.collection.createIndex() for the missing index, then re-execute the query to force a fresh plan.** (85% success)
   ```
   Run db.collection.getPlanCache().clear() followed by db.collection.createIndex() for the missing index, then re-execute the query to force a fresh plan.
   ```
2. **Upgrade to MongoDB 7.0+ where plan cache invalidation is more robust; if not possible, set internalQueryPlanCacheSize to 0 in the configuration to disable caching temporarily.** (80% success)
   ```
   Upgrade to MongoDB 7.0+ where plan cache invalidation is more robust; if not possible, set internalQueryPlanCacheSize to 0 in the configuration to disable caching temporarily.
   ```

## Dead Ends

- **** — Restarting mongod clears the plan cache temporarily, but the error reoccurs if the underlying cause (e.g., missing index or stale statistics) isn't addressed. (90% fail)
- **** — Manually running db.collection.getPlanCache().clear() may not help if the query shape is still cached in the server's internal structures; the error persists. (70% fail)
