Shrink a SQL Server Log File: When and How to Do It
In this article
Shrink a SQL Server transaction log with DBCC SHRINKFILE (your_database_log, target_size_in_MB) after first backing up the log or switching the database to SIMPLE recovery. Shrinking without fixing what grew the log first means it will just grow right back, so the backup-and-recovery-model step is the part most people get wrong.
Time 10 min · Steps 8 · Level Medium
Run DBCC SHRINKFILE after a log backup, not instead of one. The shrink command only releases space at the far end of the log file, and the active portion of the log (the part SQL Server still needs for recovery) can never be shrunk. If the log is 50 GB because it has never been backed up, shrinking it will barely work and the space will return within days. Fix the cause, then shrink what is left.
Shrink the Log File in Eight Steps
- Find the logical file name. Run USE your_database; SELECT name, size/128.0 AS size_MB FROM sys.database_files; The row with type_desc = 'LOG' gives you the logical name you must pass to DBCC SHRINKFILE (labels and output columns can differ slightly between SQL Server versions).
- Check the recovery model. Run SELECT recovery_model_desc FROM sys.databases WHERE name = 'your_database'; FULL means the log only truncates after log backups; SIMPLE means it truncates automatically at checkpoints.
- Back up the log if you are in FULL recovery. Run BACKUP LOG your_database TO DISK = 'path\log.bak'; This marks old log space as reusable, which is what lets the shrink actually release it.
- Switch to SIMPLE recovery only if you can lose point-in-time restore. ALTER DATABASE your_database SET RECOVERY SIMPLE; is a legitimate one-off way to force truncation, but it breaks your log backup chain, so most production databases should not stay here.
- Shrink the file. USE your_database; DBCC SHRINKFILE (logical_log_name, 512); shrinks the log to about 512 MB. The target is a minimum, not a promise: SQL Server will not shrink below the active log or below the file's growth increment.
- Repeat the backup and shrink if the first pass does little. One backup-and-shrink cycle often releases only part of the space. Back up the log again, shrink again, and the virtual log files free up in waves.
- Set a sensible growth increment. ALTER DATABASE your_database MODIFY FILE (NAME = logical_log_name, FILEGROWTH = 512MB); A fixed increment avoids the default 10% growth, which gets slower and larger every time the file grows.
- Return to FULL recovery and take a full backup. If you switched to SIMPLE, run ALTER DATABASE your_database SET RECOVERY FULL; then a full database backup, because the old log chain is broken and log backups will fail until a new full backup exists.
Match the Shrink Method to Why the Log Grew
The right approach depends on the recovery model and what caused the growth, so diagnose before you shrink.
| Situation | Why the log is big | What to do |
|---|---|---|
| FULL recovery, log never backed up | Log keeps growing until a log backup marks space reusable | Schedule regular log backups (e.g. every 15-30 minutes), then shrink once |
| SIMPLE recovery, one huge transaction | A big index rebuild or bulk load held the log open | Log truncates at next checkpoint; shrink once, after the transaction finishes |
| Log grew during a long-running backup or replication lag | Something held the oldest active log record | Fix the backup or replication delay first; shrinking before that does nothing |
| Replication, mirroring or availability group pending | Undelivered transactions pin the log | Restore delivery to the secondary, then back up and shrink |
| Accidental one-off (log grew to fill the disk) | Disk pressure from a single event | Back up, shrink, and set a fixed growth increment so it cannot happen silently again |
Rule of thumb: shrink the log once, after you have fixed the reason it grew. A log that needs shrinking every week is a symptom, not a maintenance task.
Why the Shrink Refuses to Release Space
An active virtual log file at the end of the file blocks everything behind it. When DBCC SHRINKFILE returns almost immediately and the file size barely changes, the active log is sitting in the last virtual log file. Back up the log (or checkpoint in SIMPLE recovery) and shrink again; each cycle moves the active log further back and releases more space. If it still refuses, something is holding the log: an uncommitted transaction, a stuck replication or backup, or a database still in the middle of a log backup. DBCC OPENTRAN shows the oldest active transaction, which is usually the culprit.
Also check that you are targeting the right file. Passing the database name instead of the logical log file name to DBCC SHRINKFILE shrinks the data file, which is slower and reorganises data pages for no benefit.
Keep the Log From Growing Back After the Shrink
Set up log backups before you shrink anything in FULL recovery. A common schedule is a full backup nightly with log backups every 15-30 minutes during the day; the log then only needs to hold the changes since the last log backup, which is typically a small fraction of the file size that scared you. If point-in-time recovery does not matter for a database, leaving it in SIMPLE recovery is a valid choice and the log manages itself. Give the log file a fixed growth increment (such as 512 MB or 1 GB) rather than a percentage, and leave some free space on the disk so an occasional large transaction can grow the log instead of failing. Pre-growing the log to a realistic working size after the shrink, rather than shrinking to the absolute minimum, avoids the performance cost of constant regrowth.
Log Shrinking Questions Worth Knowing
Does shrinking the log lose data?
No. DBCC SHRINKFILE releases only unused, truncated log space; committed transactions are in the data file and in your backups. What you can lose is the ability to restore to a point in time if you switch to SIMPLE recovery without a full backup afterwards.
Is shrinking the log bad for performance?
The shrink itself is a one-off and mostly harmless on a log file. The real cost is the regrowth cycle: every auto-grow event takes time, and a log that repeatedly shrinks and grows fragments into many small virtual log files, which slows log backups and crash recovery.
Can I shrink the log in SSMS instead of T-SQL?
Yes. In Object Explorer, right-click the database, choose Tasks > Shrink > Files, set File type to Log, and enter a target size in MB. It runs the same DBCC SHRINKFILE command underneath, and the menu labels can differ slightly between SSMS versions.
What target size should I shrink to?
Shrink to roughly the size the log reaches between two normal log backups, then let it stay there. Shrinking a busy production log to a few megabytes guarantees it will regrow within the hour.
Official source: Microsoft Support