To shrink a large database log file in ms sql server

To shrink a large database log file, follow these steps:

Step 1: Identify the Logical Name of the Log File

Run the following query to get the logical name of the log file for your database:

USE [YourDatabaseName];
GO

SELECT name AS LogicalName, type_desc AS FileType, size * 8 / 1024 AS FileSizeMB
FROM sys.database_files;

Look for the file with type_desc = LOG.


Step 2: Shrink the Log File

Use the DBCC SHRINKFILE command to shrink the log file.

USE [YourDatabaseName];
GO

DBCC SHRINKFILE (LogicalName, TargetSizeInMB);
GO
  • Replace LogicalName with the name of your log file obtained from Step 1.
  • Replace TargetSizeInMB with the desired size (e.g., 100 MB).

Example:

DBCC SHRINKFILE (YourLogFileLogicalName, 100);
GO

Step 3: Ensure Log File Usage is Minimized

To ensure shrinking works effectively:

  1. Backup the Transaction Log (if the database is in FULL or BULK_LOGGED recovery model):

    BACKUP LOG [YourDatabaseName] TO DISK = 'C:\Backup\YourDatabase_Log.trn';
    GO
    
  2. Switch to Simple Recovery Mode Temporarily: If you don't need point-in-time recovery, you can temporarily switch to the SIMPLE recovery model:

    ALTER DATABASE [YourDatabaseName] SET RECOVERY SIMPLE;
    GO
    

    After shrinking, you can switch back to FULL if required:

    ALTER DATABASE [YourDatabaseName] SET RECOVERY FULL;
    GO
    

Step 4: Check the Log File Size Again

Verify the size after shrinking:

SELECT name AS LogicalName, type_desc AS FileType, size * 8 / 1024 AS FileSizeMB
FROM sys.database_files;

Notes:

  • Frequent shrinking is not recommended as it can lead to fragmentation.
  • If your log file is large due to uncommitted transactions or excessive logging, address those underlying issues.

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2025 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM