How to Remove Secondary Transaction Log File?

Resolved SQL
CM
Community Member
November 27, 2015
6 replies
910 views
Reviewed by moderators

I am looking to delete the secondary transaction log file. But when I try to delete the file by ALTER DATABASE REMOVE FILE command it throws me the following errorrnMsg 5042, Level 16, State 1, Line 1rnThe file 'mydatabase_Log_2' cannot be removed because it is not empty. rnHow can I delete the secondary transaction log file? Please help

6 Answers
Accepted Answer
Verified by Edwin J. Hoffer, Expert · Reviewed November 2015

Try this n

SELECT o. name AS TableOrIndexn FROM sysfiles fnJOIN dbo. sysfilegroups sn ON f. groupid = s. groupidnJOIN dbo. sysindexes in ON i. groupid = s. groupidnJOIN dbo. sysobjects on ON i. id = object_id(o. name)n AND i. indid in (0, 1)nWHERE f. name = ' My_Logical_File_Name' nUNIONnSELECT i. namen FROM sysindexes i join sysfilegroups f ON i. groupid = f. groupidn JOIN sysfiles l ON f. groupid = l. groupidnWHERE l. name = ' My_Logical_File_Name'
nn

Thanks Stephen for providing the script, I tried the script, but found no object listed. It didn’t work for me, I am still facing the same problem

Use the DBCC SHRINKFILE with the EMPTYFILE argument commandnndbcc ShrinkFile (' logical_file_name', EmptyFile)n

I have tried it but it doesn't work for me. Is there any other way?

You can also follow the below steps: nn1. Connect to the instance of SQL Server Database Engine & expand the instance. n2. Expand Databases > right-click the database from which to delete the file, and then click Properties. n3. Select the Files page. n4. In the Database files grid, select the file to delete and then click Remove. n5.Click OK. n

Thanks Andrew, this was really helpful. I had been struggling with this problem for a long time and couldn't figure it out. Your solution fixed it