Clear Query Cache: What It Means and How to Do It
Clearing the query cache means removing stored results of previously executed database queries so that subsequent identical queries are re-run against the current data instead of being served from memory. This is typically done after data changes that the cache cannot detect automatically, or when stale results are suspected. The exact command and scope depend on the database system, but the goal is always the same: force fresh reads and free the memory the cache was using.
What a Query Cache Actually Is
A query cache is a server-side memory area that stores the text of a SQL statement together with its result set. When an identical statement arrives, the server can return the stored result without parsing, optimizing, or executing the query again. This can reduce CPU load and latency for read-heavy workloads where the same queries repeat often.
Different database systems implement this differently. MySQL historically had a dedicated query cache that matched SQL text byte-for-byte; it was removed in MySQL 8.0 because of scalability problems under write-heavy loads. PostgreSQL does not have a built-in query cache in the same sense; it relies on the operating system page cache and its own shared buffers, and tools like connection poolers or external caches may add a query cache layer. Other systems, such as some data warehouses and ORMs, maintain their own result caches. So "clear query cache" can refer to a database command, an application-level cache flush, or a client-side cache reset.
When You Need to Clear It
You generally clear the query cache when cached results no longer reflect the underlying data and the cache has not invalidated itself. Common situations include:
- Bulk data changes applied outside the normal application path, such as direct SQL updates, imports, or restores, which may not trigger the cache's invalidation logic.
- Schema changes that alter tables referenced by cached queries; some systems invalidate affected entries automatically, others do not.
- Stale reads during debugging, where an application shows old values even though the database contains new ones.
- Memory pressure, when the cache has grown large and you want to reclaim memory or reset its statistics.
- Benchmarking, to ensure you measure cold-cache performance or to compare cached versus uncached execution.
It is not something to do routinely on a schedule. Frequent clearing defeats the purpose of the cache and can increase load on the database.
How to Clear It in Common Systems
The method depends on where the cache lives. The following are the standard approaches for widely used systems; always check the documentation for your exact version, because syntax and availability change between releases.
MySQL (versions with the query cache)
Older MySQL releases (before 8.0) provide the FLUSH QUERY CACHE statement, which defragments the cache without removing entries, and RESET QUERY CACHE, which removes all cached queries. You can also disable the cache at runtime by setting query_cache_size to zero, or set query_cache_type to OFF. In MySQL 8.0 and later, the query cache no longer exists, so there is nothing to clear.
PostgreSQL
PostgreSQL has no dedicated query cache to flush. If you are using an extension or a pooler with a cache, use that component's own reset mechanism. Otherwise, clearing the operating system page cache or restarting the server affects shared buffers, but this is a heavy operation and rarely appropriate in production.
Application and ORM caches
Many frameworks and ORMs keep a second-level cache or a prepared-statement cache. These are cleared through the framework's API, a configuration change, or by restarting the application process. For example, some ORMs expose a cache-clearing method, while others require you to disable the cache and restart.
Proxy and CDN caches
If a caching proxy or content delivery network sits in front of the database or API, its cache is cleared through the vendor's dashboard or API, not through SQL. This is a separate layer from the database query cache and is often the actual source of stale data.
Step-by-Step: Clearing Safely
Before running any cache-clearing command, confirm what you are clearing and who is affected. A reasonable sequence is:
- Identify the cache layer: database query cache, ORM cache, proxy cache, or client cache. Check configuration files and documentation.
- Check whether the cache is enabled and how large it is; many systems expose status variables for hits, misses, and memory use.
- Confirm that clearing will not cause an unacceptable load spike. On a busy system, a cold cache can send many queries to disk at once.
- Run the appropriate command or API call during a low-traffic window if possible.
- Verify the effect by repeating the query that returned stale data and checking that it now returns current results.
- Monitor database load and query latency for a short period after clearing.
If you are unsure which layer is stale, test from the client upward: run the query directly against the database, then through the application, then through any proxy. The first layer that returns old data is the one to clear.
Common Problems and Misconceptions
A frequent misconception is that clearing the query cache fixes all stale-data problems. In practice, stale data often comes from application-level caches, browser caches, or replication lag, not from the database query cache. Clearing the wrong layer wastes time and can cause unnecessary load.
Another issue is that some caches invalidate automatically when the underlying table changes, so manual clearing is redundant. Conversely, caches that match on exact SQL text can miss entries when only whitespace or parameter values differ, leading to unexpected cache misses rather than stale hits. If you see high miss rates, examine the query patterns before assuming the cache is broken.
Finally, clearing a cache is not a substitute for fixing the cause of stale reads. If data changes are not propagating, check transaction isolation, replication status, and application caching logic.
Alternatives to Clearing
Instead of clearing the whole cache, you can often target the problem more precisely:
- Invalidate specific entries if your cache supports key-based removal.
- Shorten the cache lifetime (TTL) so stale data expires sooner.
- Disable the cache temporarily to confirm whether it is the source of the issue.
- Fix invalidation logic so that writes trigger cache updates automatically.
- Use read-through or write-through caching patterns that keep the cache consistent with the database.
These approaches avoid the performance cost of a full flush and reduce the risk of a cold-cache load spike.
Frequently Asked Questions
Does clearing the query cache delete data?
No. Clearing a query cache only removes stored result sets from memory. The underlying tables and data are unaffected. The next identical query will be executed against the database and may be slower than a cache hit.
Why is my query still slow after clearing the cache?
A cold cache means the first execution must do full work: parsing, planning, and reading from disk. If the query is still slow after that, the cause is likely missing indexes, inefficient SQL, or resource contention, not the cache.
Can I clear the query cache on a production server?
Yes, but understand the impact. On a busy system, clearing the cache can cause a sudden increase in disk reads and CPU usage. Prefer targeted invalidation or a low-traffic window, and monitor load afterward.
Is the MySQL query cache still available?
No. The query cache was deprecated in MySQL 5.7 and removed in MySQL 8.0. If you are on 8.0 or later, there is no query cache to clear; performance depends on indexes, buffer pool size, and query design.
How do I know if the query cache is being used?
Most databases expose status variables for cache hits, misses, and memory usage. Check the documentation for your version. A high ratio of hits to misses suggests the cache is effective; many misses may indicate poor cache configuration or query patterns.
Does clearing the cache affect other users?
It can. A cleared cache is shared by all connections to that database instance, so every user may see slower first queries until the cache warms again. Coordinate with others before clearing on a shared system.