Saturday, July 08, 2006

How to empty a SQL Server database transaction log file

I have 1GB databases that grow several GB log files.  How do you safely clear those logs?  Well, probably the best practice includes backing them up.  That was overkill for me, and sifting through all the documentation and forums (eventually) led me to this simple pair of statements that do the trick immediately:

backup log [dbname] with truncate_only
go
DBCC SHRINKDATABASE ([dbname], 10, TRUNCATEONLY)
go

17 comments:

Michael Irwin said...

with 400 users and a report dept that suddenly started "trying things out", I had a log file almost as large as the VM I was running in.

Thanks for your blog entry ! You're a lifesaver !

Anonymous said...

Great code. Thanks. You saved me 50 GB of my server's hard drive.

Arvin said...

Thank for sharing this SQL. I've been looking for this for a very long time.

Sandeep K N said...

thanx a lot dude

andreir said...

It's my turn to say : "Thanks, man!" :)

Sebastien said...

awesome, finally someone with the answer i'm looking for...

g1ga said...

so simple so great! bro you saved my life! :D

Göran said...

Wonderful, I'm so happy :)

ansblog said...

GOD BLESS YOU, DUDE !

It's so nice to see the .ldf file just 1.25 MB ! :) thx a lot

sharjeel said...

thanks a Lot Man, I am really happy, trying to email the database since long, but fail due to file size.

Muhammad Sharjeel Ahsan

PLAST1C said...

Thanks a lot!!!

PLAST1C said...

thanks a lot man

Pakoros said...

ํYou solve my problem.
Thank you very much.

marty said...

Does this work on SQL 2008?
I tried it and got the error:

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

Rani Shoura said...

Thanks a lot !!!!

Waltee said...

Thanks Much - This info helped greatly

Ibrahim said...

I Like :)