Smart Systems for Home

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

SQL Maintenance Plan and Shrinking the Log File Safely

· 5 min read · Home Tech

In this article
  1. Read the Log File Before You Touch It
  2. Shrink the Log Once, the Right Way
  3. Match the Recovery Model to What the Log Is For
  4. Put Log Backups in the Maintenance Plan Instead of a Shrink
  5. Where Scheduled Shrinks Go Wrong
  6. Log Shrinking for Index Rebuilds and Big Batch Jobs

Shrinking the transaction log is not something you should put in a SQL Server maintenance plan at all. Shrink the log file once, manually, after you have fixed whatever made it grow; scheduling a recurring shrink re-grows the file every time and fragments it.

Time 15 min · Steps 6 · Level Medium

The mistake most people make is treating a big log file as a problem to trim on a schedule. A large log usually means the database is in FULL recovery and nobody is taking log backups, or a long-running transaction is holding the log hostage. Fix that first, shrink once, and the file stays at a stable size on its own.

Read the Log File Before You Touch It

Before shrinking anything, find out why the log grew. Run DBCC SQLPERF(LOGSPACE) to see the log's used percentage per database, and check the recovery model with a query against sys.databases. If the log is 20 GB but only 3 percent used, the space is free and a shrink will work. If it is 80 percent used, shrinking will barely release anything and you need to address what is holding the space first.

Also check for open transactions with DBCC OPENTRAN in the database's context. A single forgotten transaction left open overnight can keep gigabytes of log from being truncated.

Shrink the Log Once, the Right Way

  1. Back up the log first. In FULL or BULKED_LOGGED recovery, take a transaction log backup so the inactive part of the log can be released. Without this step the shrink does almost nothing.
  2. Switch to SIMPLE recovery only if you accept losing point-in-time restore. If this database does not need log backups (a dev box, a reporting copy), SIMPLE recovery truncates the log automatically. Skip this on production databases that need point-in-time recovery.
  3. Run the shrink against the log file, not the database. Use DBCC SHRINKFILE (logical_log_name, target_in_MB), naming the log file specifically. Shrinking the whole database with DBCC SHRINKDATABASE hits data files too, which causes data-file fragmentation.
  4. Set a sensible target size. Shrink to the size your log normally needs between log backups, not to the minimum. If the log routinely needs 8 GB, shrinking it to 512 MB just means it re-grows in bursts, and every growth event stalls your transactions while SQL Server zero-fills the new space.
  5. Check the result. Run DBCC SQLPERF(LOGSPACE) again and confirm the file size and used percentage. If the file did not shrink, the log is still held by something active, not broken.
  6. Right-size growth settings. Set the log's autogrowth to a fixed increment in MB rather than a percentage, and enable instant file initialization does not apply to log files, so keep increments moderate, for example 512 MB to 1 GB, so growth is quick but not constant.

Match the Recovery Model to What the Log Is For

The recovery model decides whether the log ever gets truncated, so it decides whether shrinking makes any sense at all.

Recovery modelLog truncationLog backupsShrink makes sense?
SIMPLEAutomatic, after checkpointsNot allowedOnce, after an abnormal growth event
FULLOnly after a log backupRequired on a scheduleOnce, after log backups are running
BULK_LOGGEDAfter a log backupRequired on a scheduleOnce, same as FULL

Rule of thumb to remember: the log shrinks itself once you let it truncate; you only shrink the file when it is physically bigger than it will ever need to be again.

Put Log Backups in the Maintenance Plan Instead of a Shrink

The maintenance plan wizard does not need a shrink task, but it absolutely benefits from a log backup schedule if your databases are in FULL recovery. A workable pattern for a mid-size production database:

  • Full backup daily, in a maintenance plan subplan.
  • Differential backup every 6 to 12 hours if the database changes a lot.
  • Transaction log backup every 15 to 60 minutes, as a separate SQL Server Agent job rather than a maintenance plan task, since the wizard's log backup options are limited.

Once log backups run on a schedule, the log gets truncated after each one and its size settles at whatever your busiest interval needs. That is the size to shrink to, once, and then leave alone.

Where Scheduled Shrinks Go Wrong

A recurring shrink task creates a growth-shrink-growth cycle that costs you twice. Each regrowth stalls transactions while SQL Server allocates and zero-initializes the new log space, and the constant file churn shows up as warnings in the SQL Server error log. You also lose the ability to predict disk needs, because the file's size no longer reflects reality.

If your motivation is disk pressure, the better move is to verify the log's used percentage first. A log that is genuinely full of active transactions will not shrink no matter how many times the task runs, and the real fix is either more frequent log backups or tracking down the long-running transaction.

Log Shrinking for Index Rebuilds and Big Batch Jobs

After an index maintenance window

Rebuilding every index in FULL recovery can balloon the log. If that growth was a one-off, take a log backup, shrink once back to the normal working size, and consider switching index maintenance to rebuilds done in smaller batches so the log never has to hold the whole operation.

After a one-time bulk load

A big import can grow the log far beyond its everyday needs. Same pattern: log backup, single shrink to the working size, then leave it. Do not let the maintenance plan inherit the shrink.

On SIMPLE recovery databases

Here the log truncates on its own, so a permanently huge log almost always traces back to a long-running transaction blocking truncation. Find it with DBCC OPENTRAN or the sys.dm_tran_database_transactions view, deal with it, then shrink once.

When the shrink refuses to release space

Run DBCC SQLPERF(LOGSPACE) to confirm the used percentage is actually low, and check for replication, log shipping, or an old active transaction pinning the log. The shrink command is not failing; the log genuinely cannot be released yet. Also confirm you are shrinking the log file by name, not the data file, which is a surprisingly common mix-up when both files have similar names.

Official source: Microsoft Support