SQL transaction log files should be maintained automatically by SQL Server, but sometimes grow too large when automatic maintenance is not being performed correctly. When this happens, transaction log files can grow unexpectedly large. It is important to take manual steps to shrink these files before they consume all available disk space.
How to Shrink a SQL Database’s Transaction Log File:
NOTE: This process should be run for each database with a large transaction log file.
- Open SQL Server Management Studio and login to the proper SQL instance.
- In the Object Explorer tree, expand the Databases folder, and select the database which has a large .LDF file.
-
Create full backup of database
- Right-click on the database and select Tasks >> Back Up….
- Make sure Backup type is set to Full.
- Remove any existing destinations, then add a new Disk destination. Browse to a location with plenty of free disk space, and name your backup file with the .BAK extension.
NOTE: This backup will only be needed for this process, and can be deleted in a day or two. - On the Options page, select bullet option for Overwrite all existing backup sets.
- Click on OK button to start backup.
-
Create Transaction Log backup of database
- Right-click on the database and select Tasks >> Back Up….
- Make sure Backup type is set to Transaction Log.
- You can keep the existing .BAK destination created in step #3c.
- On the Options page, select bullet option for Overwrite all existing backup sets.
- Click on OK button to start backup.
-
Shrink Transaction Log File
- Right-click on the database and select Tasks >> Shrink >> Files.
- For File Type, select Log.
- Click OK button.
- Repeat steps 3, 4, and 5 until the .LDF file is noticeably smaller.
How to Check Size of Transaction Log File
NOTE: This process should be run for each database.
- Open SQL Server Management Studio and login to the proper SQL instance. If there are multiple instances, you may need to repeat this process for each.
- In the Object Explorer tree, expand the Databases folder, and select one of the production databases. Right-click on the database and select Properties.
- In the Database Properties window, click on the Files page and make note of the path of the files.
- Open Windows Explorer and browse to the path obtained in step #3.
- Check for any transaction log files larger than their data files.
Data files are named with an .MDF extension.
Transaction log files are typically named with an .LDF extension. These files are typically smaller than the .MDF file they belong to. If a transaction log file is more than 20% larger than its .MDF counterpart, you will need to shrink the file.