Microsoft Certified IT Professional Rudi Bruchez EI Microsoft Certified IT Professional

Rudi Bruchez EI · www.pachadata.com · rudi@pachadata.com · +33 6 22 48 76 99
Consulting, audit, performance tuning, solutions, training
SQL Server OLTP and BI / NoSQL

Version of October 14, 2026 (4308dcaa) · https://www.pachadata.com/en/docs/howtos/system-databases/move-tempdb-files/

Move tempdb files

Generate and run the ALTER DATABASE script that moves the tempdb files to another drive, effective when the SQL Server service restarts.

The move is done with a script. To generate that script, you can use this small query:

DECLARE @newFolder NVARCHAR(MAX) = 'D:\Data'

SELECT
  CONCAT('ALTER DATABASE tempdb MODIFY FILE (NAME = [' + f.name + '],',
    ' FILENAME = ''', @newFolder , '\',
    reverse(left(reverse(physical_name), charindex('\', reverse(physical_name)) -1)),
    ''');') as [ddl]
FROM sys.master_files f
WHERE f.database_id = DB_ID(N'tempdb');

This code is also on my GitHub.

Copy the generated code and run it in an SSMS window.

You will see a message like this one for each file moved:

The file "tempdev" has been modified in the system catalog. The new path will be used the next time the database is started.

The move therefore takes effect when the SQL Server service is restarted. Tempdb is recreated at that point, and you can then delete the old files from the old directory.

Contact

I have been an independent SQL Server consultant since 2006. I help when an application slows down, when a server costs too much in storage or licenses, or when a migration must not stop production: audit and diagnosis, query and index tuning, migrations, high availability, training. On site, or as a remote audit delivered in five working days.

rudi@pachadata.com · +33 6 22 48 76 99 · www.pachadata.com/en/contact/