Smart Systems for Home

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

SQL Shrink Log File Doesn't Shrink: Why and the Fix

· 4 min read · Home Tech

In this article
  1. Why the Shrink Does Nothing
  2. Steps That Actually Shrink It
  3. The Misconception and the Fact
  4. Signs You Are Hitting Each Cause
  5. What to Do After It Shrinks

DBCC SHRINKFILE only trims inactive virtual log files from the end of the transaction log, so if the active part of the log sits at the far end, the file barely shrinks. You first need to clear the log (usually with a log backup), then shrink, and often repeat the shrink a few times.

Time 15 min · Steps 6 · Level Advanced

Why the Shrink Does Nothing

The transaction log is not one continuous file inside SQL Server; it is divided into virtual log files (VLFs). DBCC SHRINKFILE can only remove VLFs from the end of the physical file, and only ones that are inactive. If the active VLF, or a VLF still holding a transaction SQL Server has not released, sits near the end of the file, there is nothing at the tail to trim and the file size stays the same even though the command reports success.

The second common cause is that the log has not actually been cleared. In the full or bulk-logged recovery model, the log only empties when you take a log backup. Shrinking never clears the log; it only reclaims space that has already been cleared. Check log_reuse_wait in sys.databases to see what is holding the log: a value of LOG_BACKUP means a log backup is due, ACTIVE_TRANSACTION means an open transaction, and REPLICATION or DATABASE_SNAPSHOT_CREATION point to other processes you must let finish first.

Steps That Actually Shrink It

  1. Check the recovery model. Run SELECT name, recovery_model_desc FROM sys.databases. If it is FULL and you are not taking log backups, either start a backup schedule or switch to SIMPLE if point-in-time recovery is not needed.
  2. Clear the log. In FULL recovery, run BACKUP LOG YourDatabase TO DISK = 'path\log.bak'. In SIMPLE recovery, a checkpoint normally clears it automatically; run CHECKPOINT if needed.
  3. Look at the VLF layout. Run DBCC LOGINFO('YourDatabase'). Each row is a VLF; the Status column shows 2 for the VLF holding the active log. If a Status 2 row sits near the last rows, that is why the shrink fails.
  4. Shrink in small increments. Run DBCC SHRINKFILE(YourDatabase_log, target_in_MB), then run DBCC LOGINFO again and repeat the shrink. Each pass can wrap the active VLF further toward the start of the file, freeing more tail space.
  5. Verify the result. Check the file size with SELECT name, size/128 AS SizeMB FROM sys.database_files, and confirm the used pages dropped with DBCC SQLPERF(LOGSPACE).
  6. Reset expectations. If the target size is below the file's minimum, SQL Server stops at the minimum and reports it in the output, so read the messages, not just the return code.

The Misconception and the Fact

The misconception is that shrinking is a cleanup operation, like emptying a recycle bin. The correct fact: shrinking is purely a space-reclamation operation on the physical file, and it depends entirely on the logical log being empty at the tail. A log that keeps growing because it is never backed up will never stay shrunk; shrink it and it simply grows again, fragmenting into many small VLFs as it does.

Signs You Are Hitting Each Cause

  • The shrink reports success but the file size is unchanged: the active VLF is at the end of the file, or the target is below the file minimum.
  • The log grows back within hours or days after shrinking: the log is not being cleared regularly, so fix the backup schedule rather than shrinking again.
  • log_reuse_wait shows ACTIVE_TRANSACTION: a long-running transaction is holding the log; find it in sys.dm_tran_database_transactions or the oldest running session and let it finish or roll it back.
  • DBCC LOGINFO returns hundreds of rows: the log has auto-grown in tiny increments and is heavily fragmented, which itself slows log backup and restore times.
  • You are in SIMPLE recovery and the log still will not shrink: run CHECKPOINT, then shrink again; a pending checkpoint keeps VLFs active.

What to Do After It Shrinks

Set the log file to a sensible fixed size so it does not auto-grow in small increments, and leave auto-growth on with a growth increment in the hundreds of megabytes rather than a percentage or a tiny default. If point-in-time recovery matters, take regular log backups, because that is what keeps the log from ballooning in the first place. Treat shrinking as a one-off repair after an incident, such as a massive one-time transaction or a switch to SIMPLE recovery, not as routine maintenance.

Menu names and dialog labels in SQL Server Management Studio, such as Tasks > Shrink > Files, can differ slightly between versions, but the DBCC commands and catalog views above are stable across versions.

Official source: Microsoft Support