Dec 21, 2009

How to truncate log file in SQL Server

I was once dealing with a simple sql server project which had a database of around 25 MB but its log file was 100+ MB and I had to download it from the production server to development machine for testing.

This is one of the situations when we should truncate the log file. Here's a code snippet which I have used with SQL Server 2005 and which works.

Say you have a database called ABCD and its files are ABCD.mdf and ABCD_log.ldf

Now log into sql server 2005, open a new query window in ABCD database, paste and run following commands

DBCC SHRINKFILE('ABCD_log', 1)
BACKUP LOG ABCD WITH TRUNCATE_ONLY
DBCC SHRINKFILE('ABCD_log', 1)
GO
Make sure to put in the name according to your database.
Also note the number '1' means log will be truncated to 1 MB.

No comments:

Post a Comment