Aug 22
Automated fix for SQL .ldf file growing out of control
If you work with MS SQL server and have databases that handle lots of transactions then you no doubt have encountered the .ldf file growing to insane sizes if left unchecked. A large LDF file will result in the web application running extremely slow or most likely just timing out, not to mention affecting the performance of other databases setup on that same server. We used to manually detach the database rename the .ldf, once you re-attach the .mdf file it would start up a new .ldf log file and things would run speedy once again. While that worked it resulted in downtime, wasted resources, and unhappy clients.
Here is a solution that works perfectly and is automated. (assuming sql server 2000 or 2005 standard here)
- Launch Enterprise Manager and open Management
- Right click on Database Maintenance Plans > New plan
- Pick all your options to backup your database with the schedule and save the plan
- Drop down SQL Server Agent
- Right click on Jobs > New Job
- Give it a name and click on the second tab “steps“
- Add a second step under the initial backup by clicking New
- Choose Type TSQL, pick the database, and paste in this command
- Save it, click on step 1 hit adavanced and set “On success action:” to “Goto Step: 2” or it won’t run.
BACKUP LOG dbnamehere WITH TRUNCATE_ONLY
DBCC SHRINKFILE(dbnamehere_Log, 1)
We have a log file that used to run to 18 to 30GB quickly and now it is 1MB on a daily basis, pretty sweet and with no real down time. We have tried backing up the log and tried the built in shriking options with no luck, never worked. Sure there are plenty of other methods you can use, hope this helps someone else out there facing the same problem.

November 11th, 2008 at 5:44 am
Hi, We have this problem and the log file reach 27 GB, so if I apply the above steps the log file size will reduce in size?..
Thank you
November 11th, 2008 at 10:38 am
Yep, it will reduce it to probably less than 3MB when done. Give it a shot. As usual run a full backup first just in case you made some wrong selections.
November 12th, 2008 at 3:19 am
thanks for your reply,
I did the maintenance plan and the job.
The sql server agent should be started? or it will start automatically, and shall we schedule the job or not.
November 12th, 2008 at 12:05 pm
Yes the sql server agent must be running or the maintenance plan won’t execute. And yes I would setup a schedule to run however often you need it to. We run it nightly on our high volume databases only.
November 13th, 2008 at 8:26 am
Hi, Hope i’am not bothering you..I followed all the mentioned steps but it didn’t work, nothing start running, even that i schedule them all.
do you have any idea what shall i do..if you want any details i can give you.
thanks a lot for your help!!