Smart Systems for Home

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

Unable to Shrink Log File in SQL Server? Here's Why

· 4 min read · Home Tech

In this article
  1. The Shrink Sequence That Actually Works
  2. Why the File Refuses to Get Smaller
  3. The Misconception: Shrinking Frees Log Space
  4. Each log_reuse_wait_desc Value and Its Fix
  5. When the Log Is Stuck at One Size
  6. Preventing the Next Runaway Log

A SQL Server log file won't shrink because the log is still in use: an active transaction, log backups not being taken, or replication/availability group reuse holding the log. Free the log for reuse first, then run DBCC SHRINKFILE with the logical file name, not the physical name.

Time 15 min · Steps 6 · Level Medium

The Shrink Sequence That Actually Works

  1. Check log reuse. Run SELECT name, log_reuse_wait_desc FROM sys.databases and read the reason: ACTIVE_TRANSACTION, LOG_BACKUP, AVAILABILITY_REPLICA, REPLICATION and others each need a different fix before shrink can work.
  2. Take a log backup. In full or bulk-logged recovery, the log only clears after a log backup: BACKUP LOG MyDb TO DISK = 'path.bak'. In simple recovery, a checkpoint clears inactive log automatically.
  3. Get the logical file name. Run SELECT name, size FROM sys.database_files WHERE type_desc = 'LOG' and use that name (often MyDb_log) in the shrink command.
  4. Shrink once, with a target. DBCC SHRINKFILE (MyDb_log, 256) targets 256 MB. Shrinking to 1 or 0 asks SQL Server to shrink as far as it can, which may still be limited by an active log block.
  5. Check the result. Query sys.database_files again or look at the file size in Object Explorer to confirm the shrink took effect.
  6. Stop the growth cycle. Set a sensible autogrowth increment (for example 512 MB) or a fixed log size so the file does not balloon and get shrunk repeatedly, which fragments the log.

Why the File Refuses to Get Smaller

The log is a ring of virtual log files, and SQL Server can only truncate the inactive part. DBCC SHRINKFILE removes only the free space at the physical end of the file. If the last virtual log file is still active, or the head of the log has wrapped around to the end, nothing can be trimmed no matter how many times you run the command. The log_reuse_wait_desc column tells you exactly which blocker applies, so start there instead of guessing.

The Misconception: Shrinking Frees Log Space

Many people believe DBCC SHRINKFILE truncates the log. It does not. Truncation (marking log space reusable) happens after a log backup or a checkpoint in simple recovery; shrink only returns already-free space to the operating system. If you shrink without fixing the reuse blocker, the file grows right back. Also, running shrink repeatedly as routine maintenance is a known anti-pattern: it causes file-system fragmentation and slows log writes, so treat shrink as a one-off cleanup after an exceptional event, not a scheduled job.

Each log_reuse_wait_desc Value and Its Fix

Match the value from sys.databases to the fix:

  • ACTIVE_TRANSACTION - a long-running transaction holds the log. Find it with DBCC OPENTRAN (MyDb) or sys.dm_tran_session_transactions, and either let it finish or, if it is orphaned, kill the session.
  • LOG_BACKUP - a log backup is due. Take one; in full recovery the log clears only at backup time.
  • BACKUP_LOG or ACTIVE_BACKUP_OR_RESTORE - a backup is running; wait for it to finish.
  • REPLICATION - the distributor has not read the log. Fix the log reader agent or remove the publication if it is no longer needed.
  • AVAILABILITY_REPLICA - a secondary replica in an availability group is behind. Check the replica's synchronization state and connectivity.
  • NOTHING - the log is reusable; shrink should now work, possibly after one more log backup to clear the tail.

When the Log Is Stuck at One Size

A common frustration: the shrink runs without error but the file barely moves. Two causes dominate. First, the target size is below the space genuinely in use, so SQL Server shrinks to the minimum it can and stops. Second, a single active virtual log file sits at the physical end of the file. Taking a log backup, running the shrink, then taking another log backup and shrinking again often releases the tail. In an emergency on a development server only, switching to simple recovery with ALTER DATABASE MyDb SET RECOVERY SIMPLE, checkpointing, shrinking, then returning to full recovery and taking a full backup will reset the log - but this breaks the backup chain, so never do it on a production database you care about restoring.

Preventing the Next Runaway Log

Pick the recovery model that matches your backup plan. Full recovery with regular log backups (for example every 15 minutes on a busy database) keeps the log small because each backup truncates the inactive part. Simple recovery suits databases where point-in-time restore is not needed and lets checkpoints clear the log. Set autogrowth to a fixed increment like 512 MB rather than a percentage, and monitor with a query on sys.dm_db_log_space_usage so you see growth before it becomes an incident. A well-managed log never needs shrinking.

Official source: Microsoft Support