Clearing an SQL cache can help when you are testing database performance, troubleshooting a stale execution plan, or checking how a query behaves without cached data. However, “SQL cache” can mean several different things. In Microsoft SQL Server, the most common caches are the plan cache, which stores compiled query plans, and the buffer cache, which holds data and index pages in memory.
The right way to clear a cache depends on your database platform and what you are trying to test. This guide explains how to clear SQL Server caches safely, how to remove a single cached plan instead of flushing everything, and what to know about MySQL and PostgreSQL. Because clearing a cache can temporarily slow down an application, use these commands carefully—especially on a production server.
What Does “SQL Cache” Mean?
A database uses memory to avoid repeating expensive work. SQL Server, for example, may retain compiled execution plans so it can reuse them when similar statements run again. It also keeps recently accessed data and index pages in memory so it does not need to read them from disk every time.
- Plan cache: Stores compiled execution plans. Clearing it forces SQL Server to compile plans again as queries run.
- Buffer cache: Holds data and index pages in memory. Clearing it means SQL Server may need to fetch those pages from disk again.
- Application or client cache: Some applications, drivers, and frameworks cache query results or metadata separately from the database server.
These caches serve different purposes. If a query is using a poor execution plan, clearing the plan cache may be relevant. If you need to measure cold-read performance in a controlled test, you may also need to clear SQL Server’s buffer cache. Neither action should be treated as a routine performance fix.
How to Clear the SQL Server Plan Cache
To clear the plan cache for the entire SQL Server instance, run the following command in SQL Server Management Studio or another connection with the required permissions:
DBCC FREEPROCCACHE;
This removes cached execution plans from memory. As queries run again, SQL Server compiles new plans and repopulates the cache. The command does not delete tables or database files, but it can cause a temporary performance impact: frequently used queries may take longer while their plans are compiled again, and overall CPU use may rise during recompilation.
Use this command only when you understand its scope. On a shared server, the plan cache may serve many databases and applications, so clearing it can affect workloads beyond the one you are investigating. It can also hide the real cause of a performance issue rather than fix it.
Clear the Plan Cache for One Database
If you are using SQL Server 2016 or later, you can clear the procedure cache for a specific database with ALTER DATABASE SCOPED CONFIGURATION. First select the database, then run:
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
This is generally more targeted than clearing the instance-wide plan cache. It can be useful when testing a database in isolation or investigating plans associated with that database. Still, expect affected queries to compile again, and avoid running it during busy periods without a clear reason.
Check your SQL Server version and permissions before using the command. Database-scoped configuration options are not available in every version or environment, and managed database services may impose different rules.
Remove a Single Query Plan Instead of Clearing Everything
When you have identified one problematic execution plan, removing only that plan can reduce the impact on other workloads. You can locate a plan handle in the dynamic management view sys.dm_exec_cached_plans, together with the SQL text and other information. For example, this query lists cached plans and their associated SQL text:
SELECT cp.plan_handle, st.text
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) AS st;
After you identify the correct plan, pass its plan handle to DBCC FREEPROCCACHE:
DBCC FREEPROCCACHE (0x...);
Replace 0x... with the actual plan handle returned by your query. Verify the SQL text and plan carefully before removing anything. A plan handle can change, and clearing a plan does not guarantee the next compilation will produce a better plan. The query may simply compile into the same plan again.
For a repeatable fix, investigate why the plan is performing poorly. Differences in parameter values, outdated statistics, data distribution, indexes, and query design can all affect plan quality. Depending on the situation, Query Store, updated statistics, an index change, or a query rewrite may be more appropriate than repeatedly clearing plans.
How to Clear the SQL Server Buffer Cache
To test database reads with data pages removed from SQL Server’s buffer cache, you can use these commands in a test environment:
CHECKPOINT;
DBCC DROPCLEANBUFFERS;
CHECKPOINT writes modified pages from memory to the database files. DBCC DROPCLEANBUFFERS then removes clean data buffers from the buffer pool. When queries request those pages again, SQL Server may have to read them from storage.
This command is not the same as clearing the plan cache. It targets data buffers, so it is useful for controlled cold-cache testing—not for routine maintenance. It can slow down other users and applications on the same instance because they may also need to reload data from disk. It does not clear the operating system’s file cache, storage-controller cache, or every cache that may exist outside SQL Server.
Do not use DBCC DROPCLEANBUFFERS on a busy production system simply to “refresh” the database. It can create a sudden increase in disk reads and make application response times worse. Run performance comparisons in a dedicated test environment whenever possible.
Permissions and Safety Checks
Cache-clearing commands require elevated permissions, and the exact permission requirements can vary by SQL Server version. Do not grant broad administrative privileges just to make a one-time test easier. Ask a database administrator to run the command or provide a controlled, approved testing process.
Before clearing any cache, confirm the database platform, server, and scope. A command intended for a development database can have a much larger impact if it is accidentally run against a shared production instance. Check active workloads, schedule a maintenance or testing window when appropriate, and record the query or performance metric you want to compare.
- Confirm whether you need to clear plans, data buffers, or an application-level cache.
- Prefer a targeted plan removal over an instance-wide flush when one plan is the issue.
- Capture baseline performance before making the change.
- Monitor CPU, disk reads, query duration, and application errors afterward.
- Do not repeatedly clear caches as a substitute for diagnosing the underlying problem.
Can You Clear the SQL Cache in MySQL or PostgreSQL?
The answer depends on which database system you use. SQL Server commands such as DBCC FREEPROCCACHE and DBCC DROPCLEANBUFFERS do not apply to MySQL or PostgreSQL. Each platform has its own memory management and cache behavior, and there may not be a single command that clears every relevant cache.
In MySQL, the old query cache was removed in MySQL 8.0. If you are troubleshooting query performance, identify whether the behavior comes from the InnoDB buffer pool, prepared statements, an application cache, or another layer. Restarting MySQL may discard some in-memory state, but it is disruptive and is not a preferred general-purpose cache-clearing method. Use version-specific documentation and test any proposed procedure outside production first.
PostgreSQL uses shared buffers and relies partly on the operating system for file caching. It does not provide a standard built-in SQL command that simply flushes all database and operating-system caches. Restarting PostgreSQL clears some server memory, but operating-system caches may remain, and restarting affects active connections. For cold-cache benchmarks, use a controlled environment and platform-appropriate tools rather than assuming a restart creates a completely cold system.
Why Clearing the Cache May Not Fix a Slow Query
A cache flush changes what the database has stored in memory; it does not correct a missing index, inefficient join, inaccurate statistics, or an application sending unexpected parameter values. A slow query can also be caused by blocking, memory pressure, storage latency, network delays, or increased data volume. If the same problem returns after the cache repopulates, investigate those underlying causes.
In SQL Server, Query Store can help compare query performance and execution plans over time. Execution plans, wait statistics, index usage, and query duration can also provide clues. Review the evidence before changing production settings. If a plan regression is confirmed, consider a targeted remediation—such as correcting statistics or query design—rather than regularly flushing the entire cache.
Conclusion
To clear the SQL cache, first identify which cache you mean. In SQL Server, use DBCC FREEPROCCACHE to clear the instance-wide plan cache, or use ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE when you need to clear a database’s procedure cache on a supported version. For controlled cold-data testing, CHECKPOINT followed by DBCC DROPCLEANBUFFERS clears clean buffer pages, but it can affect performance across the server.
Whenever possible, clear only the specific plan you are investigating, test outside production, and monitor the impact. Cache clearing is a useful diagnostic step—not a lasting solution for a query that is slow because of an underlying design or resource issue.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.