Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Shrink a Log File in SQL Server

· 4 min read · Home Tech

In this article
  1. Shrink the Log in 7 Steps
  2. When the Shrink Refuses to Release Space
  3. Before You Start: Backups, Space and What Shrinking Cannot Do
  4. How to Tell It Worked, and What to Watch Afterwards
  5. Shrinking the Log in an Emergency

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

SymptomCauseFix
Shrink reports current size unchangedLog never backed up under FULL recoveryTake a log backup, then shrink again
log_reuse_wait_desc shows LOG_BACKUPWaiting on a backup to truncateBack up the log, then shrink
log_reuse_wait_desc shows ACTIVE_TRANSACTIONA long-running transaction holds the logFind it with sys.dm_tran_active_transactions, let it finish or kill it, then shrink
log_reuse_wait_desc shows REPLICATIONReplication or CDC needs old log recordsFix the distribution or CDC job, then shrink
File shrinks then balloons back within hoursLog growth from index rebuilds or bulk loadsSchedule 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