What is the command to truncate a SQL Server log file?

Sql ServerTruncateLogging

Sql Server Problem Overview


I need to empty an LDF file before sending to a colleague. How do I force SQL Server to truncate the log?

Sql Server Solutions


Solution 1 - Sql Server

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, then Options.
    • Make sure "Recovery model" is set to "Simple", not "Full"
    • Click OK
  • 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)

Ref: <http://msdn.microsoft.com/en-us/library/ms189493.aspx>

Solution 2 - Sql Server

if I remember well... in query analyzer or equivalent:

BACKUP LOG  databasename  WITH TRUNCATE_ONLY

DBCC SHRINKFILE (  databasename_Log, 1)

Solution 3 - Sql Server

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.

Solution 4 - Sql Server

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.

Solution 5 - Sql Server

backup log logname with truncate_only followed by a dbcc shrinkfile command

Solution 6 - Sql Server

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)

Solution 7 - Sql Server

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAidan RyanView Question on Stackoverflow
Solution 1 - Sql ServerBlorgbeardView Answer on Stackoverflow
Solution 2 - Sql ServerilaView Answer on Stackoverflow
Solution 3 - Sql ServerNathan RView Answer on Stackoverflow
Solution 4 - Sql ServerMatejView Answer on Stackoverflow
Solution 5 - Sql ServerSQLMenaceView Answer on Stackoverflow
Solution 6 - Sql Serverrip747View Answer on Stackoverflow
Solution 7 - Sql ServerRaskView Answer on Stackoverflow