Smart Systems for Home

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

SQL Server Log File Won't Shrink: Why and How to Fix It

· 5 min read · Home Tech

In this article
  1. Shrink the Log in the Right Order
  2. Why the Shrink Command Does Nothing
  3. When Switching to Simple Recovery Is the Right Fix
  4. Signs You're Solving the Wrong Problem
  5. What Shrinking Actually Costs You

The log file won't shrink because the active part of the log is still in use, or the recovery model keeps every transaction. Shrink the file only after you back up the log (or switch the database to Simple recovery), otherwise SQL Server has nothing to truncate and DBCC SHRINKFILE does almost nothing.

Time 20 min · Steps 7 · Level Medium

A transaction log grows to hold the transactions you run, and it only releases space back when SQL Server can truncate the inactive part. Truncation happens automatically after a log backup in Full or Bulk_Logged recovery, or automatically at each checkpoint in Simple recovery. Shrinking without truncating first is like squeezing a full bottle: nothing moves. The fix is always the same order — make the log truncatable, then shrink, then stop whatever made it balloon.

Shrink the Log in the Right Order

  1. Check the log's used space. Run DBCC SQLPERF(LOGSPACE) to see the log size and the percentage actually in use. If used space is high, shrinking won't help yet — the log is genuinely full of active records.
  2. Back up the transaction log. In Full recovery, run BACKUP LOG YourDatabase TO DISK='...'. This marks the inactive part of the log as reusable. In Simple recovery, skip this — a checkpoint does the job.
  3. Find the log's logical file name. Run SELECT name, size/128.0 AS SizeMB FROM sys.database_files WHERE type_desc = 'LOG' to get the exact name and current size in MB.
  4. Shrink the file. Run DBCC SHRINKFILE(YourLogName, 512) to target 512 MB (choose your own target). Never use DBCC SHRINKDATABASE for this — it shrinks data files too, which hurts performance.
  5. Check the result. Re-run DBCC SQLPERF(LOGSPACE) or query sys.database_files again. If the file shrank, you're done with the immediate problem.
  6. Find why it grew. Look at sys.databases for the recovery model, and check for long-running transactions with DBCC OPENTRAN — an old open transaction pins the log and blocks truncation.
  7. Set a growth limit. In database properties, cap the log's autogrowth at a sensible maximum (for example 2 GB increments, restricted to a few times your normal log size) so one bad query can't fill the disk.

Why the Shrink Command Does Nothing

DBCC SHRINKFILE can only release virtual log files (VLFs) from the end of the file that are inactive and unused. If it returns immediately with little or no size change, one of these is holding the log:

  • No log backup taken. In Full recovery, the log is never truncated until you back it up. A database created and then left in Full recovery with no log backups grows its log forever.
  • A long-running transaction. An uncommitted transaction hours old keeps every log record after it active, so nothing truncates.
  • Replication, mirroring or log shipping. If a secondary or distributor is behind, records wait for it and the log stays fat.
  • An active backup. A full or log backup running at the same time blocks truncation until it finishes.
  • Immediate reuse is blocked. A bulk operation under Bulk_Logged recovery keeps extents marked until the next log backup.

To see the exact reason, query sys.databases for the log_reuse_wait_desc column — it names what's holding the log, such as LOG_BACKUP, ACTIVE_TRANSACTION or REPLICATION. That one word usually tells you the whole story.

When Switching to Simple Recovery Is the Right Fix

If the database doesn't need point-in-time restore — a dev box, a reporting copy, a small app where daily full backups are enough — switch to Simple recovery and the log truncates itself at every checkpoint:

ALTER DATABASE YourDatabase SET RECOVERY SIMPLE; then take a log backup or run a checkpoint, then shrink as above.

Do not do this on a production database that requires point-in-time recovery. Switching to Simple breaks the log backup chain, and you lose the ability to restore to a moment between your full backups. If you need that, stay in Full recovery and schedule log backups every 15 minutes to an hour — frequent log backups keep the log small without any shrinking at all.

Signs You're Solving the Wrong Problem

A log that keeps regrowing after every shrink isn't a shrink problem; it's a workload or configuration problem. Look for these signs:

  • The log grows by large jumps — check autogrowth settings; growth by percentage can explode the file fast.
  • Shrink works, then within hours the file is back — you have rare but huge transactions (index rebuilds, bulk loads) or missing log backups.
  • log_reuse_wait_desc shows ACTIVE_TRANSACTION — find the long transaction with DBCC OPENTRAN and deal with it at the source.
  • The log is on a disk that's nearly full — move it to its own volume before anything else.

Repeatedly shrinking and regrowing also fragments the log into many small VLFs, which slows database startup and recovery. If the log is badly fragmented, back it up, shrink it as far as it will go, then grow it once to its final size in one large step.

What Shrinking Actually Costs You

Shrinking the log is safe for your data — you never lose committed transactions by shrinking, because only unused space is released. The cost is operational, not data loss: the file will grow again the next time the workload needs the space, and growth events are slow and can stall queries while they happen. So treat DBCC SHRINKFILE as a one-time cleanup after fixing the root cause, not a scheduled job. A scheduled shrink that runs alongside a growing workload just hides the real problem until the disk fills.

Official source: Microsoft Support