2009-06-18

Shrinking SQL Server Log Files

In SQL Server, the transaction log files grow unboundedly until you take some action.  To completely discard old transactions, perform the following steps in the database in question:

backup log databaseName to disk='nul:'
dbcc shrinkfile('logical log file name', 64)

If you actually want to save the contents of the transaction log, use a better pathname than nul.  If you do not know the logical log file name, execute this query:

select * from sys.database_files

In practice, I have found that you may have to run the BACKUP LOG command a few times to get the file to shrink down significantly.