SQL SERVER – How to Remove TempDB File?

In the last 11 years of blogging, I have never received a question which I have received yesterday. The question which I often receive is about how to add TempDB files in SQL Server but I have never received a question about how to remove a TempDB file.

One of the DBA accidently added an additional TempDB file to their setup and had no idea what to do next to go back to the original situation. Here is the screenshot of the files which he accidentally added into TempDB. The challenge which he was facing that he was not allowed to use SQL Server Management Studio (SSMS) and he has to remove this file with T-SQL only.

Here is the script which he had accidently ran to add the additional data file tempdev1.

USE [master]
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev1', FILENAME = N'C:\tempdev1.ndf')
GO

What he wanted to do is to reverse the operation by deleting the file. Here is another script which he can execute and delete the file which he accidentally added.

SQL SERVER - How to Remove TempDB File? tempdbremove-800x537

USE [tempdb]
GO
ALTER DATABASE [tempdb] REMOVE FILE [tempdev1]
GO

Well, that’s it.

Here are few additional blog posts which are related to TempDB.

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Server, SQL TempDB
Previous Post
SQL SERVER – Move Database Files for a Mirrored Database Without Breaking Mirroring
Next Post
SQL SERVER – How to Auto Start SQL Followed by a Crash?

Related Posts

Leave a Reply