Click here to Skip to main content
Click here to Skip to main content

How to Truncate Log File in SQL Server 2005

, 9 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
How to truncate log file in SQL Server 2005
Sample Image - shrink.jpg


SQL Server 2005 is quite different from SQL Server 2000. To truncate log file is one thing which is different from SQL Server 2000. In SQL Server 2000, you just use Shrink to whatever file size you like. In SQL Server 2005, sometimes I cannot shrink the log file at all.

Here I want to describe some tricks to truncate log file for a database in SQL Server 2005. The work environment is Microsoft SQL Server Management Studio.

I. Shrink the Log File Size at the Right Time

I found out this trick:

Immediately after I use the SSIS package or Import the data to the database (highlight the database->Tasks->Import data …), or Export the data from the database (highlight the database->Tasks->Export data …), I can shrink the log file to the desired size, for example, 1MB. That is, highlight the database->Tasks->Shrink->Files , set the file size, say, 1MB.

Then, click OK and you are done.

II. Eliminate the Log File Completely

Sometimes, we just do not need the big log file. For example, I have a 40GB log file. I am sure I do not need this log file and want to get rid of it completely to free up the hard drive space. The logic is:

  1. Detach the database
  2. Rename the log file
  3. Attach the database without the log file
  4. Delete the log file

Let’s say, the database name is testDev. In the SQL Server Management Studio,

  1. Highlight the database-> Tasks->Detach..-> Click OK
  2. Go to log file folder -> rename the testDev_log.ldf to be like testDev_log-aa.ldf
  3. Highlight Databases->Attach…-> Click Add -> add the database testDev, highlight the log file and click the ‘Remove’ button. This means you only attach testDev.mdf
  4. After this is done, you can verify the contents of the attached database and then delete the log file

This way we can safely delete the log file and free up the space.

If you think this is very helpful, please leave your comments online. If you have any questions or suggestions, please email me at

Happy SQLing!


  • 9th June, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author
Web Developer
United States United States
.Focus on database (SQL Server 2005/2000/7/6.5 and Oracle 10g) development with C#,
.Like to work with MS Server 2005 SSIS and report service
.Like full cycle software design, development and deployment
.Microsoft Certified SQL Server Developer and DBA.

Comments and Discussions

Questionانتا خارق PinmemberAhmed F Salameh10-Mar-14 0:43 
General[My vote of 1] My vote of 1 Pinmembermuzzi_w7-Feb-13 14:35 
QuestionVery Helpful PinmemberJacky_kooboobird5-Jun-12 18:06 
QuestionMy vote is 5 Pinmembermuhamd yusuf14-Feb-12 21:29 
GeneralMy vote of 5 PinmemberTrong Thao3-Dec-10 18:42 
Generalshrinking log file in SQL2005 PinmemberOkeola Mudashiru22-Oct-10 15:58 
GeneralMy vote of 1 Pinmemberblackr2d20-Apr-10 0:23 
GeneralRe: My vote of 1 Pinmemberfaisalcode4-Jun-10 12:52 
GeneralRe: My vote of 1 Pinmemberblackr2d5-Jul-10 23:49 
GeneralRe: My vote of 1 Pinmembershresthadk@hotmail.com4-Aug-10 17:29 
QuestionHow to create Folder or File Name with Space ? PinmemberGolden Jing22-Dec-09 1:08 
GeneralThis is not the way to shrink the file Pinmemberjunk qwe23-Nov-09 13:34 
GeneralThanks PinmemberMember 66136623-Oct-09 7:49 
GeneralMy vote of 1 PinmemberJon Kehayias9-Sep-09 18:39 
GeneralHORRIBLE ADVICE! IF YOU ARE A DBA OR ADMIN READ BELOW! PinmemberJon Kehayias9-Sep-09 18:32 
GeneralVery helpful Pinmemberlingli2004-Sep-09 10:10 
GeneralRe: Very helpful PinmemberJon Kehayias9-Sep-09 18:36 
GeneralRe: Very helpful PinmemberBasel Nimer15-Mar-10 3:45 
GeneralThank U. Pinmember>FLY<</xml>14-Aug-09 1:42 
GeneralMy vote of 1 PinmemberHoward Richards22-Jul-09 6:54 
GeneralThanks Pinmemberabii44427-May-09 9:09 
GeneralDBCC SHRINKDATABASE PinmemberMember 45889187-May-09 6:21 
GeneralThanks Pinmemberohnmartun25-Oct-08 6:25 
GeneralDetaching and deleting the log is definitely not advisable PinmemberDavid Portas8-Apr-08 13:18 
GeneralThanks to all contributers [modified] PinmemberSuper Coder!10-Sep-07 20:20 
GeneralNice tip PinmemberVishal Halbe3-May-07 5:59 
GeneralShrink log by statement Pinmemberdislektik30-Mar-07 4:10 
GeneralRe: Shrink log by statement PinmemberRavin200025-Feb-09 1:53 
GeneralGood tip - works for SQL Server 2000 as well PinmemberKeith Worden4-Dec-06 4:55 
GeneralThe equivalent T-SQL statement PinmemberMr McGoo5-Sep-06 3:35 
GeneralRe: The equivalent T-SQL statement Pinmemberjlspublic2-May-07 5:33 
GeneralRe: The equivalent T-SQL statement Pinmembersenglory12-Aug-10 15:58 
GeneralDon't trash your transaction log... PinmemberPaulRandal - MSFT17-Jun-06 2:52 
GeneralRe: Don't trash your transaction log... Pinmemberiwonder10-Sep-06 17:24 
GeneralRe: Don't trash your transaction log... Pinmemberverena_techie14-Oct-08 13:25 
Generalthanks Pinmemberamit.vaishnav15-Jun-06 23:37 
GeneralAlert: Need "Simple" recovery model Pinmemberdatapvs13-Jun-06 5:10 
GeneralFor the uninitiated PinmemberSteve_Orbiz12-Jun-06 14:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 9 Jun 2006
Article Copyright 2006 by
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid