Click here to Skip to main content
Licence CPOL
First Posted 9 Jun 2006
Views 419,060
Bookmarked 40 times

How to Truncate Log File in SQL Server 2005

By | 9 Jun 2006 | Article
How to truncate log file in SQL Server 2005
 
Part of The SQL Zone sponsored by
See Also
Sample Image - shrink.jpg

Introduction

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 hong_wei_li@yahoo.com.

Happy SQLing!

History

  • 9th June, 2006: Initial post

License

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

About the Author

hong_wei_li@yahoo.com

Web Developer

United States United States

Member

.Focus on database (SQL Server 2005/2000/7/6.5 and Oracle 10g) development with C#,
 
ASP.NET, ASP, Java, PHP.
.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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks Pinmemberohnmartun5:25 25 Oct '08  
GeneralDetaching and deleting the log is definitely not advisable PinmemberDavid Portas12:18 8 Apr '08  
GeneralThanks to all contributers [modified] PinmemberSuper Coder!19:20 10 Sep '07  
GeneralNice tip PinmemberVishal Halbe4:59 3 May '07  
GeneralShrink log by statement Pinmemberdislektik3:10 30 Mar '07  
GeneralRe: Shrink log by statement PinmemberRavin20000:53 25 Feb '09  
GeneralGood tip - works for SQL Server 2000 as well PinmemberKeith Worden3:55 4 Dec '06  
Good tip - works for SQL Server 2000 as well - deleting the log file can release tremendous amounts of space as I've never had the shrink function do a complete shrink. Most recently I ended up with a log file that was 653 megabytes (after shrink) and the database was only 1 megabyte! Doing a detach and deleting the file worked a treat.
GeneralThe equivalent T-SQL statement PinmemberMr McGoo2:35 5 Sep '06  
GeneralRe: The equivalent T-SQL statement Pinmemberjlspublic4:33 2 May '07  
GeneralRe: The equivalent T-SQL statement Pinmembersenglory14:58 12 Aug '10  
GeneralDon't trash your transaction log... PinmemberPaulRandal - MSFT1:52 17 Jun '06  
GeneralRe: Don't trash your transaction log... Pinmemberiwonder16:24 10 Sep '06  
GeneralRe: Don't trash your transaction log... Pinmemberverena_techie12:25 14 Oct '08  
Generalthanks Pinmemberamit.vaishnav22:37 15 Jun '06  
GeneralAlert: Need "Simple" recovery model Pinmemberdatapvs4:10 13 Jun '06  
GeneralFor the uninitiated PinmemberSteve_Orbiz13:30 12 Jun '06  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 9 Jun 2006
Article Copyright 2006 by hong_wei_li@yahoo.com
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid