How to Shrink TempDB Without SQL Server Restart? – Interview Question of the Week #163

Question: Can you shrink tempdb without restarting SQL Server?

A stone basin drains free water while occupied space remains

Answer: Sometimes. DBCC SHRINKFILE can reclaim space while SQL Server is online, but it may stop above the target when pages can’t be moved or released. A customer once booked an on-demand consulting call because repeated DBCC SHRINKFILE attempts were not making tempdb smaller. I asked why they needed me on the call for what sounded like a routine command. They insisted, and once I joined I understood: the command was running, but the space they hoped to reclaim was still in use.

So yes, tempdb can sometimes be shrunk while SQL Server stays online. But don’t start by clearing caches or repeatedly issuing shrink. First find which file grew, how much is used, and whether the workload is likely to need the space again. tempdb is recreated on service start at its configured size; a large current size alone is not proof of a performance problem.

USE tempdb;
SELECT name AS LogicalFileName,
       type_desc AS FileType,
       size / 128.0 AS AllocatedMB,
       FILEPROPERTY(name, 'SpaceUsed') / 128.0 AS UsedMB
FROM sys.database_files
WHERE type_desc = N'ROWS'
ORDER BY file_id;

If there is a genuine one-time space emergency, identify the affected logical file and choose a target that still supports normal workload. In the example below, tempdev must actually be the logical file name, and 1,024 MB must be an appropriate target in your environment.

-- Example only, after measuring usage and identifying this file.
USE tempdb;
DBCC SHRINKFILE (N'tempdev', 1024);

A shrink may stop above the target when pages remain in use or concurrent tempdb activity gets in the way. Investigate the workload and repeat the measurement, not the command in a loop. A quiet period improves the chance that the shrink reaches its target.

Clearing Caches Is Not a Shrink Step

You’ll often see DBCC FREEPROCCACHE or DBCC DROPCLEANBUFFERS suggested next to a tempdb shrink. They do different things. FREEPROCCACHE clears cached plans; DROPCLEANBUFFERS removes clean data pages from the buffer pool. Neither belongs in a shrink recipe. Clearing the plan cache can trigger widespread recompilation, so it’s not a harmless alternative to a restart.

I would rather fix the cause of unexpected growth than celebrate a smaller number for one afternoon.

tempdb grew: Before you shrink tempdb

A big tempdb is not the problem, it is the symptom, so measure what filled it before you shrink it.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Shrinking Database, SQL Scripts, SQL Server, SQL TempDB
Previous Post
How to Reduce High Virtual Log File (VLF) Count? – Interview Question of the Week #162
Next Post
What is Alternative to CASE Statement in SQL Server? – IIF Function – Interview Question of the Week #164

Related Posts

8 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.