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 What to Do

· 4 min read · Home Tech

In this article
  1. The Shrink Sequence That Actually Works
  2. Why the Log Refuses to Give Space Back
  3. What log_reuse_wait_desc Tells You
  4. Rule of Thumb and the Big Misconception
  5. When the Shrink Runs but the File Barely Moves

A SQL Server log file won't shrink because the log is not reusable yet: either the database is in FULL recovery and no log backups have been taken, or something is holding an active transaction or virtual log file at the end of the file. DBCC SHRINKFILE only trims the inactive tail, so you must first make that space reusable, then shrink.

Time 20 min · Steps 7 · Level Medium

The Shrink Sequence That Actually Works

  1. Check the log's reuse reason. Query sys.databases for the log_reuse_wait_desc column for your database; it tells you exactly what is holding the log (common values: LOG_BACKUP, ACTIVE_TRANSACTION, REPLICATION).
  2. Back up the log if recovery model is FULL. Under FULL or BULK_LOGGED recovery, the log only clears after a transaction log backup; without one, the log grows forever and never shrinks. BACKUP LOG DatabaseName TO DISK = 'path'.
  3. Resolve active transactions. Long-running or orphaned transactions keep the log from clearing; check sys.dm_tran_database_transactions for the oldest open transaction and either commit, roll it back, or kill the session.
  4. Switch to SIMPLE if you don't need point-in-time recovery. ALTER DATABASE DatabaseName SET RECOVERY SIMPLE clears the log on checkpoint; only do this if log backups are not part of your recovery plan.
  5. Shrink the file to a target size. DBCC SHRINKFILE (DatabaseName_log, 1024) targets 1024 MB; use the logical file name from sys.database_files, which is not always the name you expect.
  6. Repeat backup and shrink if needed. One pass may only free part of the tail; a second log backup plus shrink moves the remaining inactive virtual log files.
  7. Reset the log's auto-growth. After shrinking, set a sensible growth increment (for example 512 MB) so the file does not grow in tiny 10 percent steps, which fragments the log badly.

Why the Log Refuses to Give Space Back

The transaction log is not one big blob; it is a ring of virtual log files (VLFs). SQL Server can only truncate from the front of the ring, and DBCC SHRINKFILE can only remove inactive VLFs at the physical end of the file. If the active part of the log sits near the end, nothing can be trimmed until log backups or a checkpoint mark those VLFs reusable. This is why shrinking before fixing the reuse reason does nothing: the command runs, reports success or a small reduction, and the file stays the same size.

Under SIMPLE recovery, the log clears automatically at checkpoint, so an oversized log there usually points to a long-running transaction or a bulk operation that momentarily pinned the log. Under FULL recovery, the log is only cleared by a log backup, so a database that has never had one since it was created can hold an enormous log that no amount of shrinking will touch.

What log_reuse_wait_desc Tells You

Before doing anything, run SELECT name, log_reuse_wait_desc FROM sys.databases. The value is your diagnosis. LOG_BACKUP means take a log backup. ACTIVE_TRANSACTION means find and finish the blocking transaction. REPLICATION or DATABASE_SNAPSHOT_CREATION means an external process is holding the log; fix that process, not the log. NOTHING means the log is already clear and a shrink should work right away.

Rule of Thumb and the Big Misconception

Rule of thumb: fix the reuse wait first, shrink second, resize auto-growth third. Never shrink as step one.

The common misconception is that shrinking the log reclaims space you will keep. In most databases the log grew for a reason, and without regular log backups (or SIMPLE recovery) it will simply grow back to the same size, causing repeated grow-shrink cycles that fragment the VLF structure and slow database startup and restores. Shrinking is a one-off fix for an accident, such as a runaway transaction or a one-time bulk load, not a maintenance routine.

When the Shrink Runs but the File Barely Moves

Two situations produce a stubborn file. First, the active VLF is at the end of the file; take another log backup (or checkpoint under SIMPLE), then shrink again, sometimes two or three cycles. Second, you are shrinking the wrong file or using the wrong logical name; check the file_id and name columns in sys.database_files and pass the logical name, not the physical path. Also confirm you are running the shrink in the context of that database, not master.

A word of caution: if the log is genuinely full and the database is in FULL recovery, repeatedly shrinking without scheduling log backups is treating the symptom. Set up a log backup job at a sensible interval, and the log stops growing out of control on its own.

Official source: Microsoft Support