SQL SERVER – Error Msg 5042, Level 16 – The File ‘tempdev02’ Cannot be Removed Because it is Not Empty

As a SQL Server DBA you would know that sometimes it is possible to make mistakes with the database. Few of the time it can be corrected. Out of them, some are easy and some are difficult. I was running out of space on my VM and I started hunting for a bigger file on my machine. It was not difficult to find and recall that I added some files to the TempDB database while doing some testing. Here is the command to get details about the database file, which gave me Error Msg 5042.

sp_helpdb tempdb

When I tried to remove the file via the UI and got below error.

SQL SERVER - Error Msg 5042, Level 16 – The File 'tempdev02' Cannot be Removed Because it is Not Empty remove-fail-01

Also tried via T-SQL below

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

And got the same error

Msg 5042, Level 16, State 1, Line 8
The file ‘tempdev02’ cannot be removed because it is not empty.

SQL SERVER - Error Msg 5042, Level 16 – The File 'tempdev02' Cannot be Removed Because it is Not Empty remove-fail-02

This was not a production environment so it was easy for me to restart, but looking at the message, it looks like it would work, if I make the file empty. So I ran below command

USE [tempdb]
GO
DBCC SHRINKFILE (N'tempdev02', EMPTYFILE)
GO

It was successful and then I was able to remove the file.

SQL SERVER - Error Msg 5042, Level 16 – The File 'tempdev02' Cannot be Removed Because it is Not Empty remove-fail-03

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

SQL Error Messages, SQL Server, SQL TempDB
Previous Post
SQL SERVER – FIX: Setup Was Not Able to Access Domain
Next Post
SQL SERVER – Unable to Start SQL Server – TDSSNIClient Initialization Failed with error 0xd

Related Posts

Leave a Reply