Move tempdb files

How to move the tempdb files to another drive

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.