Question: I had to add a second log file to my database because the log file ran out of space and I was unable to perform a transaction log backup. Now I can’t drop the second log file. Can you help?
Answer: Adding a second transaction log file temporarily is quite acceptable when the log file runs out of space. If the log cannot be cleared (usually by a transaction log backup) then the alternatives really come down to adding another log file or switching to the simple recovery model (and breaking the log backup chain).
Once the crisis has passed, the second log file should be removed again. In this case, when the ALTER DATABASE was issued to drop the second log file, the error returned was:
Looking at log_reuse_wait_desc in sys.databases to see why the log cannot be cleared shows NOTHING.
So how can this be? Well, it could be that the currently active VLF (Virtual Log File) happens to be in the second file – there always has to be at least one active VLF. Looking at DBCC LOGINFO to see the VLF status shows the following:
FileId FileSize StartOffset FSeqNo Status Parity CreateLSN
------ -------- ----------- ------ ------ ------ ---------
2 1245184 8192 25 2 64 0
3 1245184 8192 0 0 0 0
Ah – this is the problem! The log files each only have a single VLF, and the entire log must always have at least two VLFs. This means the second log file cannot be dropped as that would leave the entire log with only a single VLF.
The trick here is to grow the first log file so that it has more than one VLF and then the second log file will be able to be dropped. If you want to know what all the DBCC LOGINFO output means, see my blog post.
But how did the database get into this state in the first place? Turns out that it’s pretty simple to engineer this case and it involves shrinking each log file when they have VLFs larger than 1MB. The script below will produce the one-VLF-per-file case I’ve described here: