I need to empty an LDF file before sending to a colleague. How do I force SQL Server to truncate the log?
7 Answers
In management studio:
- Don't do this on a live environment, but to ensure you shrink your dev db as much as you can:
- Right-click the database, choose
Properties
, thenOptions
. - Make sure "Recovery model" is set to "Simple", not "Full"
- Click OK
- Right-click the database, choose
- Right-click the database again, choose
Tasks
->Shrink
->Files
- Change file type to "Log"
- Click OK.
Alternatively, the SQL to do it:
ALTER DATABASE mydatabase SET RECOVERY SIMPLE
DBCC SHRINKFILE (mydatabase_Log, 1)
-
1Your answer has just saved my day! I didn't know of the "right-click - Tasks -> Shrink" option. Thank you!– RenéApr 20, 2011 at 13:51
-
7
-
1I'm no DBA, but yes, I believe that backing up the log will truncate it: technet.microsoft.com/en-us/library/ms179478.aspx Aug 26, 2013 at 22:53
-
2@JohnBubriski If you're using a recovery model other than simple, the logs are the basis for recovering data or rolling back transactions. So, in production, you'll need to backup these logs first before you can shrink the log files. Otherwise, there'd be no actual recovery possibility. Unfortunately, if you're in a recovery situation, you'll have to re-load all the transaction log backups in order to fully recover the DB. Fun times, to be sure! :)– definesAug 22, 2014 at 16:07
-
2in SQL Server 2012 I had to
use mydatabase
before executingdbcc shrinkfile
– knbSep 26, 2016 at 8:52
if I remember well... in query analyzer or equivalent:
BACKUP LOG databasename WITH TRUNCATE_ONLY
DBCC SHRINKFILE ( databasename_Log, 1)
-
2This is definitely better than setting the database recovery model to SIMPLE (as in Blorgbeard's answer) because if your recovery model is FULL, you have it set that way for a reason. Oct 19, 2010 at 17:13
-
53truncate_only is deprecated in SQL Server 2008 so you have to switch the db to simple recovery msdn.microsoft.com/en-us/library/ms143729(SQL.90).aspx Dec 15, 2010 at 22:14
-
10
-
4Adding to what net_prog said, for SQL Server 2012 I substituted the first line for
BACKUP LOG DatabaseNameHere TO DISK='NUL:'
. Nov 30, 2016 at 5:39 -
'TRUNCATE_ONLY' is not a recognized BACKUP option. (SQL Server 2019 RC1) Nov 17, 2019 at 22:03
For SQL Server 2008, the command is:
ALTER DATABASE ExampleDB SET RECOVERY SIMPLE
DBCC SHRINKFILE('ExampleDB_log', 0, TRUNCATEONLY)
ALTER DATABASE ExampleDB SET RECOVERY FULL
This reduced my 14GB log file down to 1MB.
-
5As the question is ambiguous as to which version and the accepted answer isn't applicable to SQL Server 2008 this answer is still valid regardless of age. Nov 19, 2011 at 14:46
-
Thanks, it helped me reduce a big Log file that didn't react with DBCC SHRINKFILE Oct 5, 2015 at 11:13
-
7Don't forget to change the recovery model back to FULL when you are done! Jul 14, 2016 at 20:10
-
You should backup before doing this (or any of the other truncation options). If you do a full backup and check the 'Copy only backup' in SSMS then you don't need the log anymore. (This is just a point in time backup). Nov 21, 2018 at 2:23
For SQL 2008 you can backup log to nul
device:
BACKUP LOG [databaseName]
TO DISK = 'nul:' WITH STATS = 10
And then use DBCC SHRINKFILE
to truncate the log file.
-
2This is the only one that ended up working in my situation... I got an error when trying to use backup with TRUNCATE_ONLY– TomXP411Sep 23, 2014 at 17:39
-
Note: this can take quite a while, even on SSD (it has to read the log to be able to discard it). For a 30GB log file on an moderately powered Azure VM it's taken 10 minutes to do 40%. Make sure to switch to the 'Messages' take in SSMS to see the percentage processed. Nov 21, 2018 at 2:03
Since the answer for me was buried in the comments. For SQL Server 2012 and beyond, you can use the following:
BACKUP LOG Database TO DISK='NUL:'
DBCC SHRINKFILE (Database_Log, 1)
Another option altogether is to detach the database via Management Studio. Then simply delete the log file, or rename it and delete later.
Back in Management Studio attach the database again. In the attach window remove the log file from list of files.
The DB attaches and creates a new empty log file. After you check everything is all right, you can delete the renamed log file.
You probably ought not use this for production databases.
-
4Never do this! There can be data in the log not yet committed to the data file. You would lose such data.– PaulApr 8, 2014 at 8:33
-
If, in your answer, you warn not to try it in production, it's not worth posting at all. Oct 6, 2015 at 12:00
-
10I disagree with the downvoters -- it is an option. Administrators just need to understand their scenario. For example -- there will be no 'uncommitted' data if there are no open transactions. Nov 12, 2015 at 21:42
-
3This is the only solution that worked for me. My drive was filled up and I couldn't backup or shrink and nothing else seemed to be working. Thank you!– BrianJul 24, 2017 at 16:36
-
3I agree; it's not a best practice but it's valuable a tool to have if you have no other options such as Brian's scenario. Feb 13, 2018 at 4:43