Delete, Shrink, Eliminate Transaction Log .LDF File
Posted by in SQL Server April 1, 2011 21 Comments

In some case, the Microsoft SQL Server Database Transaction Log (.LDF) file becomes very big even ton of Gigabytes. It’s wasting lot of disk space and causing some problems if you want to backup and restore the database.

To face with this issue, in this tutorial, you can apply either of following methods. I’m on Microsoft SQL Server 2008 but it also be applied for Microsoft SQL Server 2005.

1. Shrink the transaction log (.ldf) file

By using this method, you can shrink the log to the desired size, ex: 1MB

+ Right-click on the database you want to shrink => Tasks => Shrink => Files

Microsoft SQL Server 2008 Shrink

Microsoft SQL Server 2008 Shrink

+ On Shrink File box, select Log on File type drop down list and select Reorganize pages before releasing unused space then set size for the log file, ex: 1 MB as picture below:

Microsoft SQL Server 2008 Shrink Set Size Log File

Microsoft SQL Server 2008 Shrink Set Size Log File

+ Click OK and you’re done.

The good point of this method is you can shrink at the right time but sometime, it doesn’t work especially with Microsoft SQL Server 2005. Let’s try the second method below in case it’s does not work.

2. Completely delete the log file

The main idea of this method is it will delete the big log file and create a new log file with the minimum size.

Follow steps below:

1. Detach the database

Right-click on the database => Tasks => Detach…

Microsoft SQL Server 2008 Detach

Microsoft SQL Server 2008 Detach

2. Delete the big log file

This step will completely erase it from your hard drive. However, from my suggestion you should back up the log file somewhere, ex: rename it from the file location (my case is: C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA)

Microsoft SQL Server 2008 Data Folder

Microsoft SQL Server 2008 Data Folder

3. Attach the database again

As we deleted the log file, so we do attach the database without the log file. Don’t worry, SQL Server will create a new log file for you which will be of the minimum size. That’s perfect!

+ Right-click Databases => Attach…

Microsoft SQL Server 2008 Attach

Microsoft SQL Server 2008 Attach

+ On Attach Database box, click Add…

Microsoft SQL Server 2008 Attach Add

Microsoft SQL Server 2008 Attach Add

+ Browser to the database (.mdf) file then click OK

Microsoft SQL Server 2008 Browser MDF

Microsoft SQL Server 2008 Browser MDF

+ Select the log (.ldf) file then click Remove

Microsoft SQL Server 2008 Attach Remove Log

Microsoft SQL Server 2008 Attach Remove Log

+ Finally, click OK and you’re done.

Note: after finished steps above, you should double check in your Data folder to see if there is a new log file created. If yes, let delete the big log file which was backup in step 2, remember?

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • Li

    Thank you, it solved my problem.

    • hoanhuynh

      Hi Li, you’re welcome. Enjoy.

  • Shane

    Hi..
    I Have followed Delete Method
    And it Work’s

    But How would I Know That my data are now in Proper not Corrupted
    Or it is Consistent

    • hoanhuynh

      Hi Shane, you’re deleting just the log file which will not impact your data file. Anyway, please backup your database before try anything.

  • gani

    Super guzel yontem <3 ;)

  • smith

    Hi..

    is there any dis advantages by applying second method( delete log file)?

  • Sriram

    Hi hoanhuynh,

    Thanks for the wonderful helppage, it helped me in the right time to resolve my issue in SQL Server 2005.

    Regards,
    Sriram.V.S

  • Cutui

    thanks a lot man…. saved my day

    best regards

  • lalala

    THANKS MAN!!!
    VERY USEFULL ARTICLE!!!

  • http://www.book-marky.de favoriten verwalten

    of course like your web site but you have to check the spelling on several of your posts. A number of them are rife with spelling issues and I find it very troublesome to tell the reality on the other hand I will certainly come again again.

  • http://goodfinance-blog.com MandyBRADLEY34

    Specialists claim that loans help a lot of people to live their own way, because they are able to feel free to buy necessary goods. Furthermore, a lot of banks give sba loan for young and old people.

  • http://www.2jdesign.co.uk/ Website Design Sheffield

    Four score and seven minutes ago, I read a sweet article.

  • Adithya

    Regarding 2nd method ::
    The transaction logs are very important to maintain the transactional integrity of the database. Therefore, you must not delete the transaction log files even after you make a backup of your database and the transaction logs.

    So ideally speaking we have two options Truncating inactive transaction/Shrinking

    http://support.microsoft.com/kb/873235/en-us

    • admin

      Dear Adithya, really appreciate your sharing. Thank you!

  • Adrian Menendez

    Be carefull, 2nd method doesn’t work if the database has more than one log file. It’s an exceptional recovery procedure and should be taken as is.

  • Lee Garet

    thank you
    Method # 2 worked out quite nicely for our unadministered SAGA server which had a 300gig ldf file

    -lee

  • N Pozatos

    Than you. I have used the 1st Option and I managed to recover 33 GB of free space in my hard disk.

    Regards,

    Nicos Pozatos

  • MAIVINHTRUC

    HELLO !!!!!!

  • Amin

    tnX m8 u made my day ;)

  • #JohnTheLloyd

    Hoan, used method two and it worked like a charm. My expertise is more on the front end. If I can ever recipricate the favor reach out to me.

  • Peter

    Perfect for us. Thanks ;-)