Click here to Skip to main content
15,898,957 members
Articles / Database Development / SQL Server
Tip/Trick

SHRINKFILE and TRUNCATE Log File in SQL Server 2008

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Feb 2011CPOL 30.7K   5   3
SHRINKFILE and TRUNCATE Log File in SQL Server 2008
When we create a new database inside the SQL Server, it is typical that SQL Server creates two physical files in the Operating System: one with .MDF Extension, and another with .LDF Extension.
* .MDF is called as Primary Data File.
* .LDF is called as Transactional Log file.

Sometimes, it looks impossible to shrink the Truncated Log file. The following code always shrinks the Truncated Log File to minimum size possible.
SQL
USE DatabaseName
GO
ALTER DATABASE [DBName] SET RECOVERY SIMPLE WITH NO_WAIT
DBCC SHRINKFILE([DBName_log], 1)
ALTER DATABASE [DBName] SET RECOVERY FULL WITH NO_WAIT
GO

License

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


Written By
Technical Lead
India India
Having 9 years of professional software development experience in analysis, design, development, testing and implementation of enterprise web applications for automobile, Digital Marketing, Telecommunication domain with good exposure to object-oriented design, software architectures, design patterns, test-driven development and agile practices.

Area of Working : Dedicated to Microsoft .NET Technologies
Proficient in: C# , ASP.net, SQL

Software Development

Database: Microsoft SQL Server,
Development Frameworks: Microsoft .NET 1.1, 2.0, 3.5, 4.0
UI: Windows Forms, ASP.NET Web Forms and ASP.NET MVC3, JQuery
Coding: WinForm , Web Development, XHTML, XML, HTML5, Javascript, WCF

Achievements:
First prize in National Level Project competition in Academic
Winner in Dotnet contest by Microsoft
Winner in Dotnet contest by Techgig
Winner in SQL Server Solution contest by Microsoft

Comments and Discussions

 
GeneralI would shrink the log using TRUNCATEONLY but I don't think ... Pin
Keith.Badeau1-Apr-11 7:03
Keith.Badeau1-Apr-11 7:03 
I would shrink the log using TRUNCATEONLY but I don't think that further shrinking would be a good idea though--due to information lopss within the logfile itself. I have less than two years of working with databases and SQL so take this with a grain of salt. This is a method I remember from a webcast done by Kimberly Tripp of SqlSkills.com (if you want exact details and wording see the webcast, this is just the gist.

If VLFs (>50) remove by: perform transaction log backup. Then shrink the log: DBCC SHRINKFILE(logFilename, TRUNCATEONLY). The size and auto growth numbers are important to keeping the logfile size in check--everytime an autogrowth occurs, A VLF is created inside the logfile--so she recommends altering the size here so as to minimize these autogrowths in the future.

Here is the webcast link: https://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?culture=en-US&EventID=1032278586&CountryCode=US.
GeneralMore detailed: If you do not care about db-changes between y... Pin
Wolle Wagner27-Mar-11 7:18
Wolle Wagner27-Mar-11 7:18 
GeneralBut if you want to restore the database afterwards, it will ... Pin
WoutL1-Mar-11 22:56
WoutL1-Mar-11 22:56 

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

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