{"id":1075,"date":"2016-03-26T10:31:01","date_gmt":"2016-03-26T17:31:01","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/glenn\/?p=1075"},"modified":"2026-07-08T08:42:03","modified_gmt":"2026-07-08T15:42:03","slug":"eight-different-ways-to-clear-the-sql-server-plan-cache","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/","title":{"rendered":"Eight Different Ways to Clear the SQL Server Plan Cache"},"content":{"rendered":"<p>Nearly anytime you see the command DBCC FREEPROCCACHE mentioned in a blog post, magazine article or book, you usually get some sort of a scary warning about how you should not use it on a production system, or else life as we know it will end. For example, <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/database-console-commands\/dbcc-freeproccache-transact-sql\">Books Online says this<\/a>:<\/p>\n<blockquote><p>Use DBCC FREEPROCCACHE to clear the plan cache carefully. Freeing the plan cache causes, for example, a stored procedure to be recompiled instead of reused from the cache. This can cause a sudden, temporary decrease in query performance. For each cleared cachestore in the plan cache, the SQL Server error log will contain the following informational message: &#8220;SQL Server has encountered %d occurrence(s) of cachestore flush for the \u2018%s\u2019 cachestore (part of plan cache) due to \u2018DBCC FREEPROCCACHE\u2019 or \u2018DBCC FREESYSTEMCACHE\u2019 operations.&#8221; This message is logged every five minutes as long as the cache is flushed within that time interval.<\/p><\/blockquote>\n<p>I would argue that running DBCC FREEPROCCACHE does not cause that much distress with a modern processor, even on a very busy OLTP system. It will cause a pretty minor CPU spike for a few seconds on most systems as the query plans get recompiled as they are executed. It can actually be pretty useful for resetting the cached_time time for <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/system-dynamic-management-views\/sys-dm-exec-procedure-stats-transact-sql\">sys.dm_exec_procedure_stats<\/a> so that it is the same for most of the stored procedures in your normal workload. That makes it easier to pick out your most expensive queries or stored procedures on a cumulative basis when you are looking at things like total worker time or total logical reads.<\/p>\n<p>Having said all that, I want to show a few methods for clearing all or part of the plan cache that are somewhat less impactful on the system. Running DBCC FREEPROCCACHE is kind of a brute force approach, so if you are concerned about that, you can run one of the variations shown below:<\/p>\n<pre class=\"csharpcode\"><span class=\"rem\">-- Eight different ways to clear the plan cache<\/span>\r\n<span class=\"rem\">-- Glenn Berry<\/span>\r\n<span class=\"rem\">-- SQLskills.com<\/span>\r\n    \r\n\r\n\r\n<span class=\"rem\">-- Example 1 ***********************<\/span>\r\n<span class=\"rem\">-- Remove all elements from the plan cache for the entire instance <\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE;\r\n\r\n\r\n<span class=\"rem\">-- Example 2 ***********************<\/span>\r\n<span class=\"rem\">-- Flush the plan cache for the entire instance and suppress the regular completion message<\/span>\r\n<span class=\"rem\">-- \"DBCC execution completed. If DBCC printed error messages, contact your system administrator.\" <\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE <span class=\"kwrd\">WITH<\/span> NO_INFOMSGS;\r\n\r\n\r\n<span class=\"rem\">-- Example 3 ***********************<\/span>\r\n<span class=\"rem\">-- Flush the ad hoc and prepared plan cache for the entire instance<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREESYSTEMCACHE (<span class=\"str\">'SQL Plans'<\/span>);\r\n\r\n\r\n<span class=\"rem\">-- Example 4 ***********************<\/span>\r\n<span class=\"rem\">-- Flush the ad hoc and prepared plan cache for one resource pool<\/span>\r\n\r\n<span class=\"rem\">-- Get Resource Pool information<\/span>\r\n<span class=\"kwrd\">SELECT<\/span> name <span class=\"kwrd\">AS<\/span> [Resource Pool Name], cache_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [cache_memory (MB)], \r\n        used_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [used_memory (MB)]\r\n<span class=\"kwrd\">FROM<\/span> sys.dm_resource_governor_resource_pools;\r\n\r\n<span class=\"rem\">-- Flush the ad hoc and prepared plan cache for one resource pool<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREESYSTEMCACHE (<span class=\"str\">'SQL Plans'<\/span>, <span class=\"str\">'LimitedIOPool'<\/span>);\r\n\r\n\r\n<span class=\"rem\">-- Example 5 **********************<\/span>\r\n<span class=\"rem\">-- Flush the entire plan cache for one resource pool<\/span>\r\n\r\n<span class=\"rem\">-- Get Resource Pool information<\/span>\r\n<span class=\"kwrd\">SELECT<\/span> name <span class=\"kwrd\">AS<\/span> [Resource Pool Name], cache_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [cache_memory (MB)], \r\n        used_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [used_memory (MB)]\r\n<span class=\"kwrd\">FROM<\/span> sys.dm_resource_governor_resource_pools;\r\n\r\n\r\n<span class=\"rem\">-- Flush the plan cache for one resource pool<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE (<span class=\"str\">'LimitedIOPool'<\/span>);\r\n<span class=\"kwrd\">GO<\/span>\r\n\r\n\r\n<span class=\"rem\">-- Example 6 **********************<\/span>\r\n<span class=\"rem\">-- Remove all elements from the plan cache for one database (does not work in SQL Azure) <\/span>\r\n\r\n<span class=\"rem\">-- Get DBID from one database name first<\/span>\r\n<span class=\"kwrd\">DECLARE<\/span> @intDBID <span class=\"kwrd\">INT<\/span>;\r\n<span class=\"kwrd\">SET<\/span> @intDBID = (<span class=\"kwrd\">SELECT<\/span> [dbid] \r\n                <span class=\"kwrd\">FROM<\/span> master.dbo.sysdatabases \r\n                <span class=\"kwrd\">WHERE<\/span> name = N<span class=\"str\">'AdventureWorks2014'<\/span>);\r\n\r\n<span class=\"rem\">-- Flush the plan cache for one database only<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FLUSHPROCINDB (@intDBID);\r\n\r\n\r\n\r\n<span class=\"rem\">-- Example 7 **********************<\/span>\r\n<span class=\"rem\">-- Clear plan cache for the current database<\/span>\r\n\r\n<span class=\"kwrd\">USE<\/span> AdventureWorks2014;\r\n<span class=\"kwrd\">GO<\/span>\r\n<span class=\"rem\">-- Clear plan cache for the current database<\/span>\r\n<span class=\"rem\">-- New in SQL Server 2016 and SQL Azure<\/span>\r\n<span class=\"kwrd\">ALTER<\/span> <span class=\"kwrd\">DATABASE<\/span> SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;\r\n\r\n\r\n\r\n<span class=\"rem\">-- Example 8 **********************<\/span>\r\n<span class=\"rem\">-- Remove one query plan from the cache<\/span>\r\n\r\n<span class=\"kwrd\">USE<\/span> AdventureWorks2014;\r\n<span class=\"kwrd\">GO<\/span>\r\n\r\n<span class=\"rem\">-- Run a stored procedure or query<\/span>\r\n<span class=\"kwrd\">EXEC<\/span> dbo.uspGetEmployeeManagers 9;\r\n\r\n<span class=\"rem\">-- Find the plan handle for that query <\/span>\r\n<span class=\"rem\">-- OPTION (RECOMPILE) keeps this query from going into the plan cache<\/span>\r\n<span class=\"kwrd\">SELECT<\/span> cp.plan_handle, cp.objtype, cp.usecounts, \r\nDB_NAME(st.dbid) <span class=\"kwrd\">AS<\/span> [DatabaseName]<\/pre>\n<p>[text][\/text]<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">FROM<\/span> sys.dm_exec_cached_plans <span class=\"kwrd\">AS<\/span> cp <span class=\"kwrd\">CROSS<\/span> APPLY sys.dm_exec_sql_text(plan_handle) <span class=\"kwrd\">AS<\/span> st \r\n<span class=\"kwrd\">WHERE OBJECT_NAME (st.objectid)<\/span><\/pre>\n<p>[text][\/text]<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">LIKE<\/span> N<span class=\"str\">'%uspGetEmployeeManagers%'<\/span> <span class=\"kwrd\">OPTION<\/span> (RECOMPILE); \r\n\r\n<span class=\"rem\">-- Remove the specific query plan from the cache using the plan handle from the above query<\/span> \r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE (0x050011007A2CC30E204991F30200000001000000000000000000000000000000000000000000000000000000);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nearly anytime you see the command DBCC FREEPROCCACHE mentioned in a blog post, magazine article or book, you usually get some sort of a scary warning about how you should not use it on a production system, or else life as we know it will end. For example, Books Online says this: Use DBCC FREEPROCCACHE [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,156,246],"tags":[281],"class_list":["post-1075","post","type-post","status-publish","format-standard","hentry","category-sql-server-2012","category-sql-server-2014","category-sql-server-2016","tag-sql-server-plan-cache"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts\/1075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/comments?post=1075"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts\/1075\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/media?parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/categories?post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/tags?post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}