<p>I'm not a SQL expert, and I'm reminded of the fact every time I need to do something beyond the basics. I have a test database that is not large in size, but the transaction log definitely is. How do I clear out the transaction log?</p>

[{'answer_id': 56647, 'author': 'Johnno Nolan', 'author_id': 1116, 'author_profile': 'https://Stackoverflow.com/users/1116', 'pm_score': 5, 'selected': False, 'text': '<p>Here is a simple and <strong>very inelegant</strong> &amp; <strong>potentially dangerous</strong>  way. </p>\n\n<ol>\n<li>Backup DB</li>\n<li>Detach DB</li>\n<li>Rename Log file</li>\n<li>Attach DB</li>\n<li>New log file will be recreated</li>\n<li>Delete Renamed Log file.</li>\n</ol>\n\n<p>I\'m guessing that you are not doing log backups. (Which truncate the log). My advice is to change recovery model from <a href="http://msdn.microsoft.com/en-us/library/ms190217.aspx" rel="noreferrer">full</a> to <a href="http://msdn.microsoft.com/en-us/library/ms191164.aspx" rel="noreferrer">simple</a>. This will prevent log bloat.</p>\n'}, {'answer_id': 61544, 'author': 'Leo Moore', 'author_id': 6336, 'author_profile': 'https://Stackoverflow.com/users/6336', 'pm_score': 2, 'selected': False, 'text': '<p><strong>To Truncate the log file:</strong></p>\n\n<ul>\n<li>Backup the database</li>\n<li>Detach the database, either by using Enterprise Manager or by executing : <em>Sp_DetachDB [DBName]</em></li>\n<li>Delete the transaction log file. (or rename the file, just in case)</li>\n<li>Re-attach the database again using: <em>Sp_AttachDB [DBName]</em></li>\n<li>When the database is attached, a new transaction log file is created.</li>\n</ul>\n\n<p><strong>To Shrink the log file:</strong></p>\n\n<ul>\n<li>Backup log [DBName] with No_Log</li>\n<li><p>Shrink the database by either:</p>\n\n<p>Using Enterprise manager :-\nRight click on the database, All tasks, Shrink database, Files, Select log file, OK.</p>\n\n<p>Using T-SQL :-\n<em>Dbcc Shrinkfile ([Log_Logical_Name])</em></p></li>\n</ul>\n\n<p>You can find the logical name of the log file by running sp_helpdb or by looking in the properties of the database in Enterprise Manager.</p>\n'}, {'answer_id': 63423, 'author': 'Jonathan', 'author_id': 6910, 'author_profile': 'https://Stackoverflow.com/users/6910', 'pm_score': 5, 'selected': False, 'text': '<p>If you do not use the transaction logs for restores (i.e. You only ever do full backups), you can set Recovery Mode to "Simple", and the transaction log will very shortly shrink and never fill up again. </p>\n\n<p>If you are using SQL 7 or 2000, you can enable "truncate log on checkpoint" in the database options tab. This has the same effect.</p>\n\n<p>This is not recomended in production environments obviously, since you will not be able to restore to a point in time.</p>\n'}, {'answer_id': 122201, 'author': 'shmia', 'author_id': 20573, 'author_profile': 'https://Stackoverflow.com/users/20573', 'pm_score': 2, 'selected': False, 'text': '<p>To my experience on most SQL Servers there is no backup of the transaction log.\nFull backups or differential backups are common practice, but transaction log backups are really seldom.\nSo the transaction log file grows forever (until the disk is full).\nIn this case the <strong>recovery model</strong> should be set to "<strong>simple</strong>".\nDon\'t forget to modify the system databases "model" and "tempdb", too.</p>\n\n<p>A backup of the database "tempdb" makes no sense, so the recovery model of this db should always be "simple".</p>\n'}, {'answer_id': 459055, 'author': 'Simon_Weaver', 'author_id': 16940, 'author_profile': 'https://Stackoverflow.com/users/16940', 'pm_score': 8, 'selected': False, 'text': "<p><strong>DISCLAIMER:</strong> Please read comments below carefully, and I assume you've already read the accepted answer. As I said nearly 5 years ago:</p>\n\n<blockquote>\n  <p>if anyone has any comments to add for situations when this is NOT an\n  adequate or optimal solution then please comment below</p>\n</blockquote>\n\n<hr>\n\n<ul>\n<li><p>Right click on the database name.</p></li>\n<li><p>Select Tasks → Shrink → Database</p></li>\n<li><p>Then click <kbd>OK</kbd>!</p></li>\n</ul>\n\n<p>I usually open the Windows Explorer directory containing the database files, so I can immediately see the effect.</p>\n\n<p><em>I was actually quite surprised this worked! Normally I've used DBCC before, but I just tried that and it didn't shrink anything, so I tried the GUI (2005) and it worked great - freeing up 17&nbsp;GB in 10 seconds</em></p>\n\n<p>In Full recovery mode this might not work, so you have to either back up the log first, or change to Simple recovery, then shrink the file.  [thanks @onupdatecascade for this]</p>\n\n<p>--</p>\n\n<p>PS: I appreciate what some have commented regarding the dangers of this, but in my environment I didn't have any issues doing this myself especially since I always do a full backup first. So please take into consideration what your environment is, and how this affects your backup strategy and job security before continuing. All I was doing was pointing people to a feature provided by Microsoft!</p>\n"}, {'answer_id': 522481, 'author': 'mrdenny', 'author_id': 4197, 'author_profile': 'https://Stackoverflow.com/users/4197', 'pm_score': 3, 'selected': False, 'text': '<p>This technique that John recommends is not recommended as there is no guarantee that the database will attach without the log file. Change the database from full to simple, force a checkpoint and wait a few minutes. The SQL Server will clear the log, which you can then shrink using DBCC SHRINKFILE.</p>\n'}, {'answer_id': 985032, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>First check the database recovery model. By default, SQL Server Express Edition creates a database for the simple recovery\nmodel (if I am not mistaken).</p>\n\n<p>Backup log DatabaseName With Truncate_Only:</p>\n\n<pre><code>DBCC ShrinkFile(yourLogical_LogFileName, 50)\n</code></pre>\n\n<p>SP_helpfile will give you the logical log file name.</p>\n\n<p>Refer to:</p>\n\n<p><em><a href="https://support.microsoft.com/en-us/help/873235/recover-from-a-full-transaction-log-in-a-sql-server-database" rel="nofollow noreferrer">Recover from a full transaction log in a SQL Server database</a></em></p>\n\n<p>If your database is in Full Recovery Model and if you are not taking TL backup, then change it to SIMPLE.</p>\n'}, {'answer_id': 2898241, 'author': 'ripvlan', 'author_id': 349079, 'author_profile': 'https://Stackoverflow.com/users/349079', 'pm_score': 3, 'selected': False, 'text': '<p>Use the <code>DBCC ShrinkFile ({logicalLogName}, TRUNCATEONLY)</code> command. If this is a test database and you are trying to save/reclaim space, this will help.</p>\n\n<p>Remember though that TX logs do have a sort of minimum/steady state size that they will grow up to. Depending upon your recovery model you may not be able to shrink the log - if in FULL and you aren\'t issuing TX log backups the log can\'t be shrunk - it will grow forever. If you don\'t need TX log backups, switch your recovery model to <em>Simple</em>.</p>\n\n<p>And remember, never ever under any circumstances delete the log (LDF) file! You will pretty much have instant database corruption. Cooked!  Done! Lost data! If left "unrepaired" the main MDF file could become corrupt permanently.</p>\n\n<p>Never ever delete the transaction log - you will lose data! Part of your data is in the TX Log (regardless of recovery model)... if you detach and "rename" the TX log file that effectively <em>deletes</em> part of your database.</p>\n\n<p>For those that have deleted the TX Log you may want to run a few checkdb commands and fix the corruption before you lose more data.</p>\n\n<p>Check out Paul Randal\'s blog posts on this very topic, <em><a href="http://sqlskills.com/BLOGS/PAUL/category/Bad-Advice.aspx#p4" rel="nofollow noreferrer">bad advice</a></em>.</p>\n\n<p>Also in general do not use shrinkfile on the MDF files as it can severely fragment your data. Check out his Bad Advice section for more info ("Why you should not shrink your data files")</p>\n\n<p>Check out Paul\'s website - he covers these very questions. Last month he walked through many of these issues in his <em>Myth A Day</em> series.</p>\n'}, {'answer_id': 4322358, 'author': 'gautam saraswat', 'author_id': 526238, 'author_profile': 'https://Stackoverflow.com/users/526238', 'pm_score': 0, 'selected': False, 'text': '<ol>\n<li>Backup DB</li>\n<li>Detach DB</li>\n<li>Rename Log file</li>\n<li>Attach DB (while attaching remove renamed .ldf (log file).Select it and remove by pressing Remove button)</li>\n<li>New log file will be recreated</li>\n<li>Delete Renamed Log file.</li>\n</ol>\n\n<p>This will work but it is suggested to take backup of your database first.</p>\n'}, {'answer_id': 4584599, 'author': 'Muhammad Imran', 'author_id': 561245, 'author_profile': 'https://Stackoverflow.com/users/561245', 'pm_score': 1, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>USE DatabaseName\n\nGO\n\nDBCC SHRINKFILE( TransactionLogName, 1)\n\nBACKUP LOG DatabaseName WITH TRUNCATE_ONLY\n\nDBCC SHRINKFILE( TransactionLogName, 1)\n\nGO \n</code></pre>\n'}, {'answer_id': 4656532, 'author': 'Peter Nazarov', 'author_id': 479858, 'author_profile': 'https://Stackoverflow.com/users/479858', 'pm_score': -1, 'selected': False, 'text': "<p>DB Transaction Log <strong>Shrink to min size</strong>:</p>\n\n<ol>\n<li>Backup: Transaction log</li>\n<li>Shrink files: Transaction log</li>\n<li>Backup: Transaction log</li>\n<li>Shrink files: Transaction log</li>\n</ol>\n\n<p>I made tests on several number of DBs: <strong>this sequence works</strong>. </p>\n\n<p>It usually <strong>shrinks to 2MB</strong>.</p>\n\n<p>OR by a script:</p>\n\n<pre><code>DECLARE @DB_Name nvarchar(255);\nDECLARE @DB_LogFileName nvarchar(255);\nSET @DB_Name = '&lt;Database Name&gt;';               --Input Variable\nSET @DB_LogFileName = '&lt;LogFileEntryName&gt;';         --Input Variable\nEXEC \n(\n'USE ['+@DB_Name+']; '+\n'BACKUP LOG ['+@DB_Name+'] WITH TRUNCATE_ONLY ' +\n'DBCC SHRINKFILE( '''+@DB_LogFileName+''', 2) ' +\n'BACKUP LOG ['+@DB_Name+'] WITH TRUNCATE_ONLY ' +\n'DBCC SHRINKFILE( '''+@DB_LogFileName+''', 2)'\n)\nGO\n</code></pre>\n"}, {'answer_id': 6387629, 'author': 'Ibrahim', 'author_id': 803446, 'author_profile': 'https://Stackoverflow.com/users/803446', 'pm_score': 2, 'selected': False, 'text': '<ol>\n<li>Take a backup of the MDB file.</li>\n<li>Stop SQL services</li>\n<li>Rename the log file</li>\n<li>Start the service</li>\n</ol>\n\n<p>(The system will create a new log file.)</p>\n\n<p>Delete or move the renamed log file.</p>\n'}, {'answer_id': 7952692, 'author': 'Rui Lima', 'author_id': 565977, 'author_profile': 'https://Stackoverflow.com/users/565977', 'pm_score': 8, 'selected': False, 'text': '<pre><code>-- DON\'T FORGET TO BACKUP THE DB :D (Check [here][1]) \n\n\nUSE AdventureWorks2008R2;\nGO\n-- Truncate the log by changing the database recovery model to SIMPLE.\nALTER DATABASE AdventureWorks2008R2\nSET RECOVERY SIMPLE;\nGO\n-- Shrink the truncated log file to 1 MB.\nDBCC SHRINKFILE (AdventureWorks2008R2_Log, 1);\nGO\n-- Reset the database recovery model.\nALTER DATABASE AdventureWorks2008R2\nSET RECOVERY FULL;\nGO\n</code></pre>\n\n<p>From: <em><a href="http://msdn.microsoft.com/en-us/library/ms189493.aspx" rel="noreferrer">DBCC SHRINKFILE (Transact-SQL)</a></em></p>\n\n<p>You may want to backup first.</p>\n'}, {'answer_id': 14628788, 'author': 'Rachel', 'author_id': 302677, 'author_profile': 'https://Stackoverflow.com/users/302677', 'pm_score': 3, 'selected': False, 'text': '<p>Most answers here so far are assuming you do not actually need the Transaction Log file, however if your database is using the <code>FULL</code> recovery model, and you want to keep your backups in case you need to restore the database, then <em>do not</em> truncate or delete the log file the way many of these answers suggest.</p>\n\n<p>Eliminating the log file (through truncating it, discarding it, erasing it, etc) will break your backup chain, and will prevent you from restoring to any point in time since your last full, differential, or transaction log backup, until the next full or differential backup is made.</p>\n\n<p>From the <a href="http://msdn.microsoft.com/en-us/library/ms186865%28v=sql.90%29.aspx" rel="noreferrer">Microsoft article on<code>BACKUP</code></a></p>\n\n<blockquote>\n  <p>We recommend that you never use NO_LOG or TRUNCATE_ONLY to manually\n  truncate the transaction log, because this breaks the log chain. Until\n  the next full or differential database backup, the database is not\n  protected from media failure. Use manual log truncation in only very\n  special circumstances, and create backups of the data immediately.</p>\n</blockquote>\n\n<p>To avoid that, backup your log file <strong>to disk</strong> before shrinking it. The syntax would look something like this:</p>\n\n<pre><code>BACKUP LOG MyDatabaseName \nTO DISK=\'C:\\DatabaseBackups\\MyDatabaseName_backup_2013_01_31_095212_8797154.trn\'\n\nDBCC SHRINKFILE (N\'MyDatabaseName_Log\', 200)\n</code></pre>\n'}, {'answer_id': 15476656, 'author': 'Michael Dalton', 'author_id': 2182233, 'author_profile': 'https://Stackoverflow.com/users/2182233', 'pm_score': 6, 'selected': False, 'text': '<p>Below is a script to shrink the transaction log, but I’d definitely recommend backing up the transaction log before shrinking it.</p>\n\n<p>If you just shrink the file you are going to lose a ton of data that may come as a life saver in case of disaster. The transaction log contains a lot of useful data that can be read using a third-party transaction log reader (it can be read manually but with extreme effort though).</p>\n\n<p>The transaction log is also a must when it comes to point in time recovery, so don’t just throw it away, but make sure you back it up beforehand.</p>\n\n<p>Here are several posts where people used data stored in the transaction log to accomplish recovery:</p>\n\n<ul>\n<li><p><em><a href="https://stackoverflow.com/questions/4507509">How to view transaction logs in SQL Server 2008</a></em></p></li>\n<li><p><em><a href="https://stackoverflow.com/questions/9767054">Read the log file (*.LDF) in SQL Server 2008</a></em></p></li>\n</ul>\n\n<p>&nbsp;</p>\n\n<pre><code>USE DATABASE_NAME;\nGO\n\nALTER DATABASE DATABASE_NAME\nSET RECOVERY SIMPLE;\nGO\n--First parameter is log file name and second is size in MB\nDBCC SHRINKFILE (DATABASE_NAME_Log, 1);\n\nALTER DATABASE DATABASE_NAME\nSET RECOVERY FULL;\nGO\n</code></pre>\n\n<p>You may get an error that looks like this when the executing commands above</p>\n\n<blockquote>\n  <p>“Cannot shrink log file (log file name) because the logical\n      log file located at the end of the file is in use“</p>\n</blockquote>\n\n<p>This means that TLOG is in use. In this case try executing this several times in a row or find a way to reduce database activities.</p>\n'}, {'answer_id': 18292136, 'author': 'Aaron Bertrand', 'author_id': 61305, 'author_profile': 'https://Stackoverflow.com/users/61305', 'pm_score': 11, 'selected': True, 'text': '<p>Making a log file smaller should really be reserved for scenarios where it encountered unexpected growth which you do not expect to happen again. If the log file will grow to the same size again, not very much is accomplished by shrinking it temporarily. Now, depending on the recovery goals of your database, these are the actions you should take.</p>\n\n<h1>First, take a full backup</h1>\n\n<p>Never make any changes to your database without ensuring you can restore it should something go wrong.</p>\n\n<h1>If you care about point-in-time recovery</h1>\n\n<p>(And by point-in-time recovery, I mean you care about being able to restore to anything other than a full or differential backup.)</p>\n\n<p>Presumably your database is in <code>FULL</code> recovery mode. If not, then make sure it is:</p>\n\n<pre><code>ALTER DATABASE testdb SET RECOVERY FULL;\n</code></pre>\n\n<p>Even if you are taking regular full backups, the log file will grow and grow until you perform a <em>log</em> backup - this is for your protection, not to needlessly eat away at your disk space. You should be performing these log backups quite frequently, according to your recovery objectives. For example, if you have a business rule that states you can afford to lose no more than 15 minutes of data in the event of a disaster, you should have a job that backs up the log every 15 minutes. Here is a script that will generate timestamped file names based on the current time (but you can also do this with maintenance plans etc., just don\'t choose any of the shrink options in maintenance plans, they\'re awful).</p>\n\n<pre><code>DECLARE @path NVARCHAR(255) = N\'\\\\backup_share\\log\\testdb_\' \n  + CONVERT(CHAR(8), GETDATE(), 112) + \'_\'\n  + REPLACE(CONVERT(CHAR(8), GETDATE(), 108),\':\',\'\')\n  + \'.trn\';\n\nBACKUP LOG foo TO DISK = @path WITH INIT, COMPRESSION;\n</code></pre>\n\n<p>Note that <code>\\\\backup_share\\</code> should be on a different machine that represents a different underlying storage device. Backing these up to the same machine (or to a different machine that uses the same underlying disks, or a different VM that\'s on the same physical host) does not really help you, since if the machine blows up, you\'ve lost your database <em>and</em> its backups. Depending on your network infrastructure it may make more sense to backup locally and then transfer them to a different location behind the scenes; in either case, you want to get them off the primary database machine as quickly as possible.</p>\n\n<p>Now, once you have regular log backups running, it should be reasonable to shrink the log file to something more reasonable than whatever it\'s blown up to now. This does <em>not</em> mean running <code>SHRINKFILE</code> over and over again until the log file is 1 MB - even if you are backing up the log frequently, it still needs to accommodate the sum of any concurrent transactions that can occur. Log file autogrow events are expensive, since SQL Server has to zero out the files (unlike data files when instant file initialization is enabled), and user transactions have to wait while this happens. You want to do this grow-shrink-grow-shrink routine as little as possible, and you certainly don\'t want to make your users pay for it.</p>\n\n<p>Note that you may need to back up the log twice before a shrink is possible (thanks Robert).</p>\n\n<p>So, you need to come up with a practical size for your log file. Nobody here can tell you what that is without knowing a lot more about your system, but if you\'ve been frequently shrinking the log file and it has been growing again, a good watermark is probably 10-50% higher than the largest it\'s been. Let\'s say that comes to 200 MB, and you want any subsequent autogrowth events to be 50 MB, then you can adjust the log file size this way:</p>\n\n<pre><code>USE [master];\nGO\nALTER DATABASE Test1 \n  MODIFY FILE\n  (NAME = yourdb_log, SIZE = 200MB, FILEGROWTH = 50MB);\nGO\n</code></pre>\n\n<p>Note that if the log file is currently > 200 MB, you may need to run this first:</p>\n\n<pre><code>USE yourdb;\nGO\nDBCC SHRINKFILE(yourdb_log, 200);\nGO\n</code></pre>\n\n<h1>If you don\'t care about point-in-time recovery</h1>\n\n<p>If this is a test database, and you don\'t care about point-in-time recovery, then you should make sure that your database is in <code>SIMPLE</code> recovery mode.</p>\n\n<pre><code>ALTER DATABASE testdb SET RECOVERY SIMPLE;\n</code></pre>\n\n<p>Putting the database in <code>SIMPLE</code> recovery mode will make sure that SQL Server re-uses portions of the log file (essentially phasing out inactive transactions) instead of growing to keep a record of <em>all</em> transactions (like <code>FULL</code> recovery does until you back up the log). <code>CHECKPOINT</code> events will help control the log and make sure that it doesn\'t need to grow unless you generate a lot of t-log activity between <code>CHECKPOINT</code>s.</p>\n\n<p>Next, you should make absolute sure that this log growth was truly due to an abnormal event (say, an annual spring cleaning or rebuilding your biggest indexes), and not due to normal, everyday usage. If you shrink the log file to a ridiculously small size, and SQL Server just has to grow it again to accommodate your normal activity, what did you gain? Were you able to make use of that disk space you freed up only temporarily? If you need an immediate fix, then you can run the following:</p>\n\n<pre><code>USE yourdb;\nGO\nCHECKPOINT;\nGO\nCHECKPOINT; -- run twice to ensure file wrap-around\nGO\nDBCC SHRINKFILE(yourdb_log, 200); -- unit is set in MBs\nGO\n</code></pre>\n\n<p>Otherwise, set an appropriate size and growth rate. As per the example in the point-in-time recovery case, you can use the same code and logic to determine what file size is appropriate and set reasonable autogrowth parameters. </p>\n\n<h1>Some things you don\'t want to do</h1>\n\n<ul>\n<li><p><strong>Back up the log with <code>TRUNCATE_ONLY</code> option and then <code>SHRINKFILE</code></strong>. For one, this <code>TRUNCATE_ONLY</code> option has been deprecated and is no longer available in current versions of SQL Server. Second, if you are in <code>FULL</code> recovery model, this will destroy your log chain and require a new, full backup.</p></li>\n<li><p><strong>Detach the database, delete the log file, and re-attach</strong>. I can\'t emphasize how dangerous this can be. Your database may not come back up, it may come up as suspect, you may have to revert to a backup (if you have one), etc. etc.</p></li>\n<li><p><strong>Use the "shrink database" option</strong>. <code>DBCC SHRINKDATABASE</code> and the maintenance plan option to do the same are bad ideas, especially if you really only need to resolve a log problem issue. Target the file you want to adjust and adjust it independently, using <code>DBCC SHRINKFILE</code> or <code>ALTER DATABASE ... MODIFY FILE</code> (examples above).</p></li>\n<li><p><strong>Shrink the log file to 1 MB</strong>. This looks tempting because, hey, SQL Server will let me do it in certain scenarios, and look at all the space it frees! Unless your database is read only (and it is, you should mark it as such using <code>ALTER DATABASE</code>), this will absolutely just lead to many unnecessary growth events, as the log has to accommodate current transactions regardless of the recovery model. What is the point of freeing up that space temporarily, just so SQL Server can take it back slowly and painfully?</p></li>\n<li><p><strong>Create a second log file</strong>. This will provide temporarily relief for the drive that has filled your disk, but this is like trying to fix a punctured lung with a band-aid. You should deal with the problematic log file directly instead of just adding another potential problem. Other than redirecting some transaction log activity to a different drive, a second log file really does nothing for you (unlike a second data file), since only one of the files can ever be used at a time. <a href="http://www.sqlskills.com/blogs/paul/multiple-log-files-and-why-theyre-bad/" rel="noreferrer">Paul Randal also explains why multiple log files can bite you later</a>.</p></li>\n</ul>\n\n<h1>Be proactive</h1>\n\n<p>Instead of shrinking your log file to some small amount and letting it constantly autogrow at a small rate on its own, set it to some reasonably large size (one that will accommodate the sum of your largest set of concurrent transactions) and set a reasonable autogrow setting as a fallback, so that it doesn\'t have to grow multiple times to satisfy single transactions and so that it will be relatively rare for it to ever have to grow during normal business operations.</p>\n\n<p>The worst possible settings here are 1 MB growth or 10% growth. Funny enough, these are the defaults for SQL Server (which I\'ve complained about and <a href="https://web.archive.org/web/20140108204835/http://connect.microsoft.com:80/SQLServer/feedback/details/415343" rel="noreferrer">asked for changes to no avail</a>) - 1 MB for data files, and 10% for log files. The former is much too small in this day and age, and the latter leads to longer and longer events every time (say, your log file is 500 MB, first growth is 50 MB, next growth is 55 MB, next growth is 60.5 MB, etc. etc. - and on slow I/O, believe me, you will really notice this curve).</p>\n\n<h1>Further reading</h1>\n\n<p>Please don\'t stop here; while much of the advice you see out there about shrinking log files is inherently bad and even potentially disastrous, there are some people who care more about data integrity than freeing up disk space.</p>\n\n<p><a href="https://sqlblog.org/2009/07/27/oh-the-horror-please-stop-telling-people-they-should-shrink-their-log-files" rel="noreferrer">A blog post I wrote in 2009, when I saw a few "here\'s how to shrink the log file" posts spring up</a>.</p>\n\n<p><a href="http://www.brentozar.com/archive/2009/08/stop-shrinking-your-database-files-seriously-now/" rel="noreferrer">A blog post Brent Ozar wrote four years ago, pointing to multiple resources, in response to a SQL Server Magazine article that should <em>not</em> have been published</a>.</p>\n\n<p><a href="http://www.sqlskills.com/blogs/paul/importance-of-proper-transaction-log-size-management/" rel="noreferrer">A blog post by Paul Randal explaining why t-log maintenance is important</a> and <a href="http://www.sqlskills.com/blogs/paul/why-you-should-not-shrink-your-data-files/" rel="noreferrer">why you shouldn\'t shrink your data files, either</a>.</p>\n\n<p><a href="https://dba.stackexchange.com/questions/29829/why-does-the-transaction-log-keep-growing-or-run-out-of-space">Mike Walsh has a great answer covering some of these aspects too, including reasons why you might not be able to shrink your log file immediately</a>.</p>\n'}, {'answer_id': 27406247, 'author': 'Shashi3456643', 'author_id': 3456643, 'author_profile': 'https://Stackoverflow.com/users/3456643', 'pm_score': 2, 'selected': False, 'text': '<p>Database → right click <em>Properties</em> → file → add another log file with a different name and set the path the same as the old log file with a different file name.</p>\n\n<p>The database automatically picks up the newly created log file.</p>\n'}, {'answer_id': 30755260, 'author': 'McRobert', 'author_id': 4870511, 'author_profile': 'https://Stackoverflow.com/users/4870511', 'pm_score': 3, 'selected': False, 'text': '<p>The SQL Server transaction log needs to be properly maintained in order to prevent its unwanted growth. This means running transaction log backups often enough. By not doing that, you risk the transaction log to become full and start to grow.</p>\n<p>Besides the answers for this question I recommend reading and understanding the transaction log common myths. These readings may help understanding the transaction log and deciding what techniques to use to &quot;clear&quot; it:</p>\n<p>From <em><a href="http://www.sqlshack.com/10-important-sql-server-transaction-log-myths/" rel="noreferrer">10 most important SQL Server transaction log myths</a></em>:</p>\n<blockquote>\n<p>Myth: My SQL Server is too busy. I don’t want to make SQL Server transaction log backups</p>\n<p>One of the biggest performance intensive operations in SQL Server is an auto-grow event of the online transaction log file. By not making transaction log backups often enough, the online transaction log will become full and will have to grow. The default growth size is 10%. The busier the database is, the quicker the online transaction log will grow if transaction log backups are not created\nCreating a SQL Server transaction log backup doesn’t block the online transaction log, but an auto-growth event does. It can block all activity in the online transaction log</p>\n</blockquote>\n<p>From <em><a href="http://blog.sqlxdetails.com/transaction-log-myths/" rel="noreferrer">Transaction log myths</a></em>:</p>\n<blockquote>\n<blockquote>\n<p>Myth: Regular log shrinking is a good maintenance practice</p>\n</blockquote>\n<p>FALSE. Log growth is very expensive because the new chunk must be zeroed-out. All write activity stops on that database until zeroing is finished, and if your disk write is slow or autogrowth size is big, that pause can be huge and users will notice. That’s one reason why you want to avoid growth. If you shrink the log, it will grow again and you are just wasting disk operation on needless shrink-and-grow-again game</p>\n</blockquote>\n'}, {'answer_id': 48585660, 'author': 'Mahendra', 'author_id': 1662592, 'author_profile': 'https://Stackoverflow.com/users/1662592', 'pm_score': 2, 'selected': False, 'text': '<p>It happened with me where the database log file was of 28 GBs. </p>\n\n<p>What can you do to reduce this? \nActually, log files are those file data which the SQL server keeps when an transaction has taken place. For a transaction to process SQL server allocates pages for the same. But after the completion of the transaction, these are not released suddenly hoping that there may be a transaction coming like the same one. This holds up the space.  </p>\n\n<p>Step 1: \nFirst Run this command in the database query explored \ncheckpoint</p>\n\n<p>Step 2: \nRight click on the database \nTask> Back up\nSelect back up type as Transaction Log\nAdd a destination address  and file name to keep the backup data (.bak)</p>\n\n<p>Repeat this step again and at this time give another file name </p>\n\n<p><a href="https://i.stack.imgur.com/ELNLW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ELNLW.png" alt="enter image description here"></a></p>\n\n<p>Step 3:\nNow go to the database \nRight-click on the database </p>\n\n<p>Tasks> Shrinks> Files \nChoose File type as Log\nShrink action as release unused space</p>\n\n<p><a href="https://i.stack.imgur.com/qwPKt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qwPKt.png" alt="enter image description here"></a></p>\n\n<p>Step 4:</p>\n\n<p>Check your log file \nnormally in SQL 2014 this can be found at </p>\n\n<p>C:\\Program Files\\Microsoft SQL Server\\MSSQL12.MSSQL2014EXPRESS\\MSSQL\\DATA</p>\n\n<p>In my case, its reduced from 28 GB to 1 MB</p>\n'}, {'answer_id': 52846837, 'author': 'hey', 'author_id': 2349661, 'author_profile': 'https://Stackoverflow.com/users/2349661', 'pm_score': 0, 'selected': False, 'text': '<p>Some of the other answers did not work for me: It was not possible to create the checkpoint while the db was online, because the transaction log was full (how ironic). However, after setting the database to emergency mode, I was able to shrink the log file:</p>\n\n<pre><code>alter database &lt;database_name&gt; set emergency;\nuse &lt;database_name&gt;;\ncheckpoint;\ncheckpoint;\nalter database &lt;database_name&gt; set online;\ndbcc shrinkfile(&lt;database_name&gt;_log, 200);\n</code></pre>\n'}, {'answer_id': 59571949, 'author': 'George M Reinstate Monica', 'author_id': 7577464, 'author_profile': 'https://Stackoverflow.com/users/7577464', 'pm_score': 1, 'selected': False, 'text': '<p>Slightly updated answer, for MSSQL 2017, and using the SQL server management studio.\nI went mostly from these instructions <a href="https://www.sqlshack.com/sql-server-transaction-log-backup-truncate-and-shrink-operations/" rel="nofollow noreferrer">https://www.sqlshack.com/sql-server-transaction-log-backup-truncate-and-shrink-operations/</a></p>\n\n<p>I had a recent db backup, so I backed up the transaction log.  Then I backed it up again for good measure.\nFinally I shrank the log file, and went from 20G to 7MB, much more in line with the size of my data.\nI don\'t think the transaction logs had ever been backed up since this was installed 2 years ago..  so putting that task on the housekeeping calendar.</p>\n'}]
['https://Stackoverflow.com/questions/56628', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5469/']