Monday, February 4, 2013

Shrink log files in SQL Server 2008 (or later)

Some years ago, I blogged about the BACKUP LOG WITH TRUNCATE_ONLY commandHowever when I tried to use the same technique a few weeks ago, it no longer worked.
When I executed the following statement
BACKUP LOG Northwind WITH TRUNCATE_ONLY

I got the following error message back.

?Msg 155, Level 15, State 1, Line 1
'TRUNCATE_ONLY' is not a recognized BACKUP option.

After some research on the Internet I discovered that ‘BACKUP LOG WITH TRUNCATE_ONLY’ is a dangerous command: it empties out the contents of your SQL Server’s transaction log without really backing it up. When you use it, you lose the ability to recover to a specific point in time.Therefore Microsoft decided to remove this capability from SQL Server 2008.

The only workaround in SQL Server 2008 is to switch the database’s recovery model to simple as shown in Books Online. Afterwards you can call the following command to shrink the logs:

DBCC SHRINKFILE(<TransactionLogName>, 1)

Afterwards you can switch back to the full recovery model.

No comments: