Skip to content

How to Shrink SQL Server Transaction Log Files using DBCC SHRINKFILE

DBCC SHRINKFILE - Shrink SQL Server Transaction Log Files. How to Shrink SQL Server Transaction Log Files using DBCC SHRINKFILE.

Category
Databases
Covers
SQL Server

Sometimes SQL Server logs files acquire a large space on the disk. In that case, we need to reduce the size of the log file to claim some space. But truncating or reducing database log file in SQL Server 2008 can be quite difficult. It also has its own issues.

This article contains a set of commands to Shrink Log Files of SQL Server database.

Generally this command is I prefer to take a backup of the database before executing below command for a safer side

Shrink SQL Server Transaction Log Files

First of all, check the actual name of log files stored in SQL server records using the following SQL query. Execute these queries in using SQL Server management studio query browser.

USE DATABASE_NAME;
GO

SELECT FILE_NAME(2);
GO

Now, use the DBCC SHRINKFILE SQL command to shrink log files. The database must be running in SIMPLE recovery mode. Open the query windows in SQL Server management studio and run the following command. You

Make sure to change DATABASE_NAME with actual database name and LOG_FILE_NAME with log file name found in the above step. The below example query will shrink the log file to 1000MB.

USE DATABASE_NAME;
GO

ALTER DATABASE DATABASE_NAME
SET RECOVERY SIMPLE;
GO  

DBCC SHRINKFILE (LOG_FILE_NAME, 1000);
GO  

ALTER DATABASE DATABASE_NAME
SET RECOVERY FULL;
GO  

All done. Now check the size of the log files, I hope you will get the expected results.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top