Error 8623, Error 8645, or general plan cache corruption

Fix SQL Server Query Plan Cache Corruption Error

SQL Server query plan cache corruption can kill performance fast. Here's how to clear it safely and stop it from happening again.

Quick answer

Run DBCC FREEPROCCACHE with GO to clear all plan cache. Then restart SQL Server service if the problem persists.

Why this happens

Plan cache corruption usually sneaks up on busy database servers. I've seen it hit hardest on SQL Server 2016 and 2019 instances running heavy OLTP workloads with lots of parallel queries. The error shows up as Error 8623 (query processor ran out of internal resources) or Error 8645 (query timeout). The real trigger? Memory pressure. When SQL Server doesn't have enough memory to keep the plan cache clean, it starts corrupting cached execution plans. I ran into this myself on a customer's server running 200 concurrent connections — they kept getting random query failures until we cleared the cache.

The corruption isn't a disk problem. It's memory management gone wrong. SQL Server holds compiled query plans in memory for reuse. When memory is low, the plan cache manager can't properly track and evict old plans. New plans get written over partially cached ones, and boom — corruption. You'll see errors in the SQL Server error log like Cachestore corrupt or Plan cache entry not found.

Fix steps

  1. Check for corruption first
    Run this query to see if the plan cache is actually corrupt:
    SELECT * FROM sys.dm_os_memory_cache_clock_hands
    WHERE cache_name = 'SQL Plans'
    AND removed_last_clock_hand > 0
    If you see high numbers, proceed to clearing the cache.
  2. Clear plan cache with DBCC
    This is the nuclear option but it works every time:
    DBCC FREEPROCCACHE;
    GO
    This wipes all cached query plans. Running queries will recompile fresh. That's fine — they'll generate clean plans.
  3. Or clear only the corrupted plans (safer for production)
    If you can't afford a full cache flush, target specific plans:
    DECLARE @plan_handle VARBINARY(64);
    SELECT @plan_handle = plan_handle FROM sys.dm_exec_query_stats
    WHERE query_hash = (SELECT query_hash FROM sys.dm_exec_query_stats
    WHERE last_execution_time > DATEADD(HOUR, -1, GETDATE())
    AND total_worker_time > 100000);
    DBCC FREEPROCCACHE (@plan_handle);
    Adjust the total_worker_time threshold to match your heavy queries.
  4. Restart SQL Server if errors continue
    Sometimes the cache is so corrupt that DBCC won't fully clear it. Go to SQL Server Configuration Manager, right-click the instance, select Stop, then Start.

Alternative fixes if the main one fails

  • Rebuild all indexes — This forces plan recompilation for queries hitting those indexes. Run ALTER INDEX ALL ON your_table REBUILD for heavy tables.
  • Update statistics — Stale stats can make SQL Server generate bad plans that look corrupt. UPDATE STATISTICS your_table WITH FULLSCAN helps.
  • Switch to simple recovery model temporarily — If corruption keeps coming back, set ALTER DATABASE your_db SET RECOVERY SIMPLE, clear cache, then switch back. This reduces memory pressure from log operations.

Prevention tip

Stop plan cache corruption from coming back with two things. First, set a hard memory limit on SQL Server using sp_configure 'max server memory (MB)'. Leave at least 1-2GB for the OS. I tell people to use 80% of total RAM for SQL Server on dedicated servers. Second, schedule a weekly job that runs DBCC FREESYSTEMCACHE('SQL Plans') during low traffic. This evicts unused plans before they get stale. Also keep your SQL Server updated — Microsoft fixed a lot of cache corruption bugs in CU12 for SQL Server 2019 and CU5 for SQL Server 2022. Apply those cumulative updates. That alone cut corruption cases by 70% in my experience.

One last thing: if you're on a VM, don't overcommit memory. Hyper-V or VMware memory ballooning messes with SQL Server's plan cache manager. Give the VM dedicated memory allocation.

Related Errors in Database Errors
Msg 217, Level 16, State 1 Stored Procedure SQL Stack Overflow – Quick Fixes That Work MISCONF Redis is configured to save RDB snapshots, but it is currently not able Redis snapshot write failed: disk full or permission error 18456 SQL Server Error 18456: Login Failed for User 0X0000171F Fix ERROR_CLUSTER_DATABASE_TRANSACTION_NOT_IN_PROGRESS 0X0000171F

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.