Skip to main content
Our Tech Ideas

Shrinking TempDB in SQL Server: A Step-by-Step Guide


In SQL Server, TempDB is a system database used to store temporary objects, intermediate results of queries, and other temporary data. Over time, TempDB can grow significantly, leading to disk space issues. Shrinking TempDB can help reclaim disk space and improve overall performance. In this blog post, we will discuss the steps to shrink TempDB in SQL Server.

Step 1: Understand the implications

Before shrinking TempDB, it’s crucial to understand the potential implications. Shrinking TempDB can cause fragmentation, which may impact performance. Additionally, if TempDB is constantly growing and shrinking, it might be an indication of underlying issues that need to be addressed.

Step 2: Analyze TempDB usage

Use SQL Server Management Studio or other monitoring tools to analyze the current usage of TempDB. Identify whether TempDB is growing excessively due to poorly optimized queries, large sorts, or other factors. Resolving these issues may help reduce the need for frequent shrinking.

Step 3: Schedule a maintenance window

Choose an appropriate maintenance window during low activity periods to minimize the impact on users. TempDB shrinking can be an intensive operation and might cause performance degradation while it’s in progress.

Step 4: Check for active sessions

Before shrinking TempDB, make sure there are no active sessions or open transactions that might be using TempDB extensively. Use the following query to identify active sessions:

USE master;
EXEC sp_who2;


-- Retrieve session information from tempdb database
USE tempdb;

-- Query to retrieve session details
    s.login_name AS 'Login Name',
    DB_NAME(r.database_id) AS 'Database',
    r.command AS 'Command',
    r.status AS 'Status',
    t.text AS 'SQL Text'
    sys.dm_exec_requests AS r
    INNER JOIN sys.dm_exec_sessions AS s ON r.session_id = s.session_id
    CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
    s.is_user_process = 1;

Terminate or resolve any open transactions or sessions that are using TempDB.

Step 5: Check autogrowth settings

Ensure that the autogrowth settings for TempDB are appropriately configured to avoid frequent growth. Ideally, it’s best to set a fixed size for TempDB and monitor its usage regularly to adjust the size accordingly.

Step 6: Shrink TempDB files

To shrink TempDB, execute the following steps:

  • Connect to SQL Server Management Studio (SSMS) using an account with appropriate privileges.
  • Open a new query window and execute the following command to switch to the TempDB database:
USE tempdb;
  • Determine the current size and free space of TempDB using the following query:
  • Execute the following command to shrink the TempDB data files:
DBCC SHRINKFILE (logical_file_name, target_size);

Replace “logical_file_name” with the logical name of the TempDB data file to shrink (e.g., ‘tempdev’) and “target_size” with the desired size in MB.

  • Execute the following command to shrink the TempDB log file:
DBCC SHRINKFILE (logical_file_name, target_size);

Replace “logical_file_name” with the logical name of the TempDB log file to shrink (e.g., ‘templog’) and “target_size” with the desired size in MB.

Step 7: Monitor and adjust

After shrinking TempDB, monitor its usage and performance. Keep an eye on any recurring growth patterns and adjust the size accordingly. It’s important to strike a balance between reclaiming disk space and avoiding frequent growth/shrinking cycles.


Shrinking TempDB in SQL Server can help reclaim disk space and optimize performance. However, it’s crucial to analyze the underlying reasons for excessive growth and address them appropriately. Remember to schedule maintenance windows and monitor TempDB usage regularly to ensure optimal performance.