SQL Server: Fix – Error 5042 The file ‘FileName’ cannot be removed because it is not empty

Recently, I shared an interesting article on the move of your table from one filegroup to another file group by moving only clustered index in SQL Server.

SQL Server: Move your Table from one File Group to another File Group

I got one comment, and one of my readers faced error like “Error 5042 The file ‘FileName’ cannot be removed because it is not empty” while deleting an old file group/data file.
In this post, I am sharing a quick fix of the Error 5042.

The solution is to shrink your empty file before deleting an actual. You can use EMPTYFILE option in DBCC SHRINKFILE to remove all junk data of a file.

After that, you can remove a file from the database using below ALTER command.

Anvesh Patel