I had to add a log file to my database to do a one-time load of data in my SQL Server 2019 database in simple recovery model. I added the file with
ALTER DATABASE db ADD LOG FILE (NAME=log2,FILENAME='C:\filepath\log2.ldf',SIZE=100GB,MAXSIZE=100GB,FILEGROWTH=0MB)
Now that I've finished my load, I attempt to remove the file with
ALTER DATABASE db REMOVE FILE log2
However, I get the error message
Msg 5042, Level 16, State 2, Line 3
The file 'log2' cannot be removed because it is not empty.
Things I've tried from my research and some ideas...
I've checked DBCC loginfo(db) which shows a status of "0" for the new file, which should mean unused.
I've also checked DBCC OPENTRAN which returns "No active open transactions".
I tried taking a full backup of the database, same error.
Tried manually running CHECKPOINT command, same error.
Checking sys.dm_db_log_info(DB_ID('db')) shows that my empty file has no active VLFs.
Tried restarting the SQL instance, same error. Tried putting the database in single user mode, same error. Tried setting the database to read_only, new error:
Msg 5004, Level 16, State 3, Line 3
To use ALTER DATABASE, the database must be in a writable state in which a checkpoint can be executed.
Which suggests that my next attempt at setting the database offline will also fail... and it does:
Msg 942, Level 14, State 4, Line 3
Database 'db' cannot be opened because it is offline.
Tried shrinking the file with
DBCC SHRINKFILE(log2,0) and DBCC SHIRNKFILE(log2,EMPTYFILE)
and it got stuck around 800,000 pages.
I also tried switching to full recovery mode, taking a log backup, then removing the file which also didn't work. Switching back to simple and attempting to remove the file still gave the same error.
And it's at this point I ran out of ideas. When I mock this in a simple fashion in a separate SQL 2022 (add file, insert some rows, remove file) it works without issue.