October 21, 2009

How to delete .log file in SQL Server 2008

I've been struggling with deleting 30GB log file for my 8GB of data..
And I figure out that easiest way to do this is just detaching the database and later attaching it without the log file. however this simple approach doesn't work when I try to do it with right click detach and right click attach..
So rather than using interface, try following:
detach the database with following commands;

USE master;
go

SP_DETACH_DB 'dbname';
go

Later just go to the directory where the database reside. Usually the log file name outline is dbname_log and resides under C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA. Delete the log file under this directory.
Later attach the database with following commands:

USE master;
go
SP_ATTACH_DB 'dbname', 'C:\Program Files\....\dbname.mdf'
go

This basically attaches the database with new log file.
Finally you get rid of the this huge log file..
Don't forget to limit auto-growth if you don't want to face same problem later..

Reference sqlhacks