Web design and development with excellence

Truncate MS SQL 2008 Database Log File

By manik26 • Sep 16th, 2009 • Category: Blog

If you have a fairly busy website, your database transaction log will grow real fast. For some websites I have seen it grows to gigs; in cases I have seen it makes the server go real slow in some cases.

So you need to truncate the log files often to keep the log file smaller in size. The process I am demonstrating here is for MS SQL 2008.

–Get in the database

use Test
GO

–see current file sizes
SELECT name,size from sys.database_files

–see current recovery model
select name,recovery_model_desc from sys.databases

–According to the articles if you switch the Recovery Model to Simple inactive part of the transaction log should be removed in MS SQL 2008.
–This command will reduce the log file somewhat
Alter database Test SET Recovery simple

–check if the recovery model is changed
select name,recovery_model_desc from sys.databases

–check file sizes
select name,size from sys.database_files

–changing the recovery model to Simple might not reduce the size enough. to reduce it further to 1 MB.
DBCC SHRINKFILE (N’Test_log’ , 1)

–check file size
select name,size from sys.database_files