# 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.
>
> Source : https://www.pachadata.com/en/docs/howtos/system-databases/move-tempdb-files/
> Author : Rudi Bruchez · updated 2026-10-14
> Markdown version of the page, for tools and LLMs. The HTML page is the reference.


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

```sql
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](https://github.com/rudi-bruchez/tsql-scripts/blob/main/database-administration/ddl-generation/move-tempdb-files.sql).

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.


---

## About the author

Rudi Bruchez, independent SQL Server consultant since 2006: performance audits, tuning, migrations, high availability and training. For an audit or hands-on help on your server: https://www.pachadata.com/en/contact/ (remote audit: https://www.pachadata.com/en/services/remote-sql-server-audit/).
