Smart Systems for Home

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

SQL Log File Will Not Shrink: Why and What Actually Works

· 4 min read · Home Tech

In this article
  1. The Six Steps That Actually Shrink a Log
  2. Why Shrinking Seems to Do Nothing
  3. Simple Recovery Model: The Faster Route
  4. Rule of Thumb and the Big Misconception
  5. Keeping the Log From Bouncing Back
  6. What to Do When the Log Fills the Disk Right Now

Your SQL Server log file will not shrink because DBCC SHRINKFILE can only remove the inactive part of the log, and something is holding that space: most often the database is in FULL recovery and no log backup has been taken. Fix the log_reuse_wait, back up the log, then shrink.

Time 15 min · Steps 6 · Level Medium

The Six Steps That Actually Shrink a Log

  1. Check the recovery model. Run SELECT name, recovery_model_desc FROM sys.databases. If it says FULL (or BULK_LOGGED), the log only clears after a log backup, so shrinking before one does almost nothing.
  2. Find what is holding the log. Run SELECT name, log_reuse_wait_desc FROM sys.databases. This tells you the real reason: LOG_BACKUP, ACTIVE_TRANSACTION, REPLICATION, AVAILABILITY_REPLICA, or NOTHING.
  3. Take a log backup. With FULL recovery, run BACKUP LOG YourDb TO DISK = 'path\log.bak'. This marks the log space inactive and is the step most people skip.
  4. Shrink the file, not the database. Use DBCC SHRINKFILE (YourDb_log, 256), where 256 is the target size in MB. Avoid DBCC SHRINKDATABASE, which touches data files too.
  5. Repeat backup then shrink if needed. One pass often frees only part of the log because the active virtual log file sits in the middle. A second log backup followed by another shrink usually releases the rest.
  6. Check the result. Run DBCC SQLPERF (LOGSPACE) or query sys.database_files to see the new log size and the percentage actually used.

Why Shrinking Seems to Do Nothing

DBCC SHRINKFILE removes only inactive portions of the log, and SQL Server writes the log as a ring of virtual log files. If the active virtual log file sits at the far end of the file, the shrink can truncate little or nothing on the first try, even after a correct log backup. That is why the backup-then-shrink sequence sometimes needs a second round before the file size drops.

The other common cause is log_reuse_wait. A value of LOG_BACKUP means the log is simply waiting for a backup; ACTIVE_TRANSACTION means a long-running transaction is holding space, and you can find it with DBCC OPENTRAN. REPLICATION or AVAILABILITY_REPLICA mean an external process has not read the log yet, and the log will not clear until that catch-up happens. Shrink commands do not override any of these; they only work on space that is already free.

Simple Recovery Model: The Faster Route

If the database does not need point-in-time recovery, switching to SIMPLE recovery clears the log automatically at checkpoints. The steps are ALTER DATABASE YourDb SET RECOVERY SIMPLE, then a checkpoint occurs, then DBCC SHRINKFILE works immediately. Switching to SIMPLE breaks the log backup chain, so if you later return to FULL, take a full or differential backup right away to restart it. Never do this on a production database that relies on log shipping, transactional replication, or an availability group without checking with whoever owns the backup plan.

Rule of Thumb and the Big Misconception

Rule of thumb: the log shrinks only after the reason it grew is gone. Diagnose with log_reuse_wait_desc first, clear the wait, then shrink. Shrinking first is like bailing water with the tap still running.

The misconception is that a huge log file means something is wrong. In fact, a large log is often just the high-water mark of past activity: a one-off index rebuild, a bulk load, or a long transaction grew it once, and SQL Server kept the space. If the log regularly grows to that size, the right fix is more frequent log backups, not repeated shrinking, because every shrink-grow cycle fragments the log and slows it down.

Keeping the Log From Bouncing Back

If you shrink the log and it balloons again within days, the shrink treated the symptom. Frequent causes are log backups taken too rarely under FULL recovery (a log that only backs up once a day will grow all day), index maintenance running in one giant transaction, and replication or mirror targets falling behind. Scheduling log backups every 15 to 30 minutes is a common pattern that keeps log growth flat, and breaking large maintenance jobs into batches keeps individual transactions short. You can also set a fixed log file size so it cannot grow unchecked, once you know the normal working size.

What to Do When the Log Fills the Disk Right Now

In an emergency where the log has consumed the drive, the fastest safe relief is a log backup to another disk, which frees inactive space without destroying anything. If there is genuinely no backup need and no second disk, switching to SIMPLE recovery and shrinking is the escape hatch, accepting the broken log chain. Do not detach the database or delete the .ldf file; a detached database missing its log may not reattach cleanly, and manual file deletion can leave the database unrecoverable. After the emergency, set up the log backup schedule that would have prevented it.

Menu names and dialog labels in SQL Server Management Studio, such as the Shrink Files dialog under Tasks, can differ slightly between versions, so the T-SQL commands above are the reliable path.

Official source: Microsoft Support