Truncate

Truncate Transaction Log

Image Posted on Updated on

Hopefully, you’re not visiting¬†this page in a panic because you or your users are not able to use the Database because it’s out of space. Keep your sanity¬†and strategically plan out the database maintenance plan to prevent issues like this from surfacing.

Before you truncate the transaction log, please remember that if you want to recovery any data before the truncation, that data will no longer exists. Before you truncate the log files, you’ll need to know the exact file name. Here’s how you do that.

Go to the database properties

Database Properties
Database Properties

Under Files, you’ll see the file log name.

File Log Name
File Log Name

SQL 2005

USE MyDatabaseName
GO
BACKUP LOG MyDatabaseName WITH TRUNCATE_ONLY

— Shrink the truncated log file to 1 MB.

DBCC SHRINKFILE (MyDatabaseName_Log, 1);
GO

SQL 2008, 2012 R2, 2014

Steer away from WITH TRUNCATE_ONLY in these versions because it no longer exists.

If your database is already set in SIMPLE recovery mode, then every time you do a backup, the transaction log will be automatically truncated for you.

If your database is in FULL recovery mode, then here are a couple of steps

USE MyDatabaseName
GO
— Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (MyDatabaseName_Log, 1);
GO

If your database is already part of a maintenance place that backups the data then use this.

BACKUP LOG MyDatabaseName_Log TO DISK = N’C:\Backup\MyDatabaseNameDB.bak’
GO

Have a cup of BEETS juice and good luck!

Advertisements