How to Shrink a Log File in SQL Server
In this article
Back up the transaction log, then run DBCC SHRINKFILE with the logical file name of the log and a target size in MB. If the file barely shrinks, the reason is almost always an active log record or a recovery model that keeps growing it, so fix that first and shrink again.
Time 5 min · Steps 7 · Level Medium
Shrink the Log in 7 Steps
- Check the recovery model. Run
SELECT name, recovery_model_desc FROM sys.databases;If it says FULL, the log only clears after a log backup; if SIMPLE, it clears automatically at checkpoints. - Back up the log if the database is FULL. Run
BACKUP LOG YourDatabase TO DISK = 'path\log.bak';This marks the inactive part of the log as reusable, which is what lets the shrink actually release space. - Find the logical name of the log file. Run
USE YourDatabase; SELECT name, size/128.0 AS SizeMB FROM sys.database_files;The log file has type_desc LOG; note its name, which is often something like YourDatabase_log. - Run the shrink. Run
DBCC SHRINKFILE (YourDatabase_log, 256);where 256 is the target size in MB. Pick a target that leaves room for normal growth, not the smallest possible number. - Check the new size. Re-run the query from step 3, or look at the file size in Object Explorer under Databases > YourDatabase, right-click > Tasks > Shrink > Files to see current and available space.
- Repeat once if needed. If the file shrank only partway, take another log backup and run the same DBCC SHRINKFILE again. Sometimes the log wraps around and a second pass releases the rest.
- Set a sensible growth limit. Right-click the database > Properties > Files, click the autogrowth button for the log, and set a fixed increment such as 512 MB instead of a percentage, with a maximum size, so one runaway transaction cannot fill the disk.
When the Shrink Refuses to Release Space
Most failed shrinks come down to one of five causes, and each has a direct fix. Check them in this order, because the later ones are rarer.
| Symptom | Cause | Fix |
|---|---|---|
| Shrink reports current size unchanged | Log never backed up under FULL recovery | Take a log backup, then shrink again |
| log_reuse_wait_desc shows LOG_BACKUP | Waiting on a backup to truncate | Back up the log, then shrink |
| log_reuse_wait_desc shows ACTIVE_TRANSACTION | A long-running transaction holds the log | Find it with sys.dm_tran_active_transactions, let it finish or kill it, then shrink |
| log_reuse_wait_desc shows REPLICATION | Replication or CDC needs old log records | Fix the distribution or CDC job, then shrink |
| File shrinks then balloons back within hours | Log growth from index rebuilds or bulk loads | Schedule log backups more often, or shrink after the maintenance window, not before |
To read that wait column, run SELECT name, log_reuse_wait_desc FROM sys.databases; A value of NOTHING means the log is clear and the shrink should work.
Before You Start: Backups, Space and What Shrinking Cannot Do
- Take a full database backup first. Shrinking is normally safe, but you are doing file-level surgery on a live database, and a current backup is cheap insurance.
- Confirm you actually have a log problem. A log that is large but stable is not an emergency; a log that grows until the disk fills is. Check free space on the drive before and after.
- Know that shrinking never touches the data file unless you target it, and it cannot make the log smaller than the space currently in use. If the log is 10 GB and 9 GB is active, the best shrink result is about 1 GB of release.
- Do not shrink on a schedule. Repeated shrink-and-regrow cycles fragment the log and cost I/O every time. Shrink once after fixing the cause, then leave it alone.
How to Tell It Worked, and What to Watch Afterwards
The shrink command reports the new file size in its output, and you can confirm it by re-running the sys.database_files query: the SizeMB column should now match your target. Disk free space on the drive should rise by roughly the same amount.
Over the next few days, watch the log size. If it climbs steadily back to its old size, the underlying cause is still there, usually backup frequency. Under FULL recovery, a common pattern is hourly log backups during business hours; a log that grows fast between backups will keep growing until you back it up more often. If the database does not need point-in-time recovery, switching to SIMPLE recovery with ALTER DATABASE YourDatabase SET RECOVERY SIMPLE; stops most log growth entirely, but it breaks the log backup chain, so take a full backup right after switching.
Shrinking the Log in an Emergency
When the disk is full and the database has stopped accepting writes, speed matters more than procedure. Take a log backup to a different drive, run DBCC SHRINKFILE with a target just under the current used size, and free enough space to get the database running again. Then, once things are calm, do the full diagnosis: check log_reuse_wait_desc, fix what is holding the log, and set a maximum log size so the next runaway transaction fails with a clear error instead of filling the disk silently.
Official source: Microsoft Support