Smart Systems for Home

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

Shrink a SQL Server Log File: How and When

· 4 min read · Home Tech

In this article
  1. How Do You Shrink the Log File?
  2. Why Won't the Log Shrink?
  3. Should You Use TRUNCATEONLY or Shrink the Whole Database?
  4. Why Shrinking Repeatedly Is a Bad Habit
  5. What Size Should the Log Be After Shrinking?
  6. What About Setting the Log to a Fixed Size?
  7. Can You Shrink the Log in SSMS Without SQL?

Run DBCC SHRINKFILE (YourDatabase_log, target_size_in_MB) after backing up the transaction log, which frees the used space and lets the file shrink. The step most people miss is the log backup: without it, the log's active portion is never released and the shrink does almost nothing.

Time 5 min · Steps 6 · Level Medium

How Do You Shrink the Log File?

  1. Find the logical file name. Run SELECT name, size/128 AS size_mb FROM sys.database_files WHERE type_desc = 'LOG' to get the log's logical name and current size in megabytes.
  2. Back up the transaction log. Run BACKUP LOG YourDatabase TO DISK = 'path\log.bak' (in full or bulk-logged recovery model). This marks the log space as reusable.
  3. Shrink the file. Run DBCC SHRINKFILE (YourDatabase_log, 512) to target 512 MB, or any size at or above the currently used space.
  4. Check the result. Re-run the query from step 1, or look at the file size in Object Explorer under your database's Files properties.
  5. Repeat the backup and shrink if needed. A single pass sometimes stalls because of an active virtual log file; waiting a few minutes and repeating usually completes it.
  6. Investigate why it grew. Check for long-running transactions, log backups that never ran, or a one-off bulk operation before deciding the new size is the right one.

Why Won't the Log Shrink?

The most common reason is that the log was never backed up. In the full recovery model, the transaction log only clears after a log backup, so shrinking before backing up just moves free space around inside the file. Run the backup first, then shrink again.

Other reasons the shrink stalls: a long-running transaction still holds old log records, database mirroring or replication is holding them, or a snapshot backup is pending. You can see what is holding the log with DBCC OPENTRAN and by checking the log_reuse_wait_desc column in sys.databases for your database. If it says ACTIVE_TRANSACTION, a running query is the cause; LOG_BACKUP means you need a log backup first.

Should You Use TRUNCATEONLY or Shrink the Whole Database?

DBCC SHRINKFILE (log_name, TRUNCATEONLY) releases only the free space at the tail end of the file and moves no pages, which makes it fast but limited. A target size in megabytes, as in step 3, can do more but takes longer on large files.

Avoid DBCC SHRINKDATABASE for this job. It shrinks every file in the database, including the data files, which causes fragmentation that hurts query performance and takes time to rebuild. Shrinking only the log file with SHRINKFILE is the targeted approach.

Why Shrinking Repeatedly Is a Bad Habit

Shrinking is a one-off fix, not maintenance. If the log grew to 50 GB because of normal workload, shrinking it to 1 GB just means it grows back, and each regrowth is either slow (with auto-growth increments) or causes file-system fragmentation. Frequent shrink-and-regrow cycles also generate instant file-initialisation work on some configurations.

The better pattern is to find the cause, fix it (usually by scheduling regular log backups, for example every 15 minutes to an hour on a busy database), then shrink once to the size the log actually needs, and leave it there.

What Size Should the Log Be After Shrinking?

Target a size the log reaches between log backups, with headroom. If you back up the log every 15 minutes and it typically grows 500 MB in that window, a log of about 1-2 GB leaves comfortable room. Shrinking below what the workload needs guarantees regrowth.

For a simple recovery model database, the log clears automatically at checkpoints, so a large log usually points to a long-running transaction or a rare bulk load rather than a backup problem. In simple model you cannot take log backups, so fixing the transaction and then shrinking is the whole job.

What About Setting the Log to a Fixed Size?

After shrinking, you can prevent regrowth by setting a maximum file size in the database's file properties (Object Explorer, right-click the database, then Files), or by limiting auto-growth to a fixed increment rather than a percentage. A fixed increment such as 512 MB is generally kinder to performance than 10 percent growth on a large file, because percentage growth gets bigger as the file does.

Do not set the maximum so low that the log can hit it during normal work. If the log fills completely and cannot grow, writes stop and the database effectively pauses until space is freed, which is far worse than a large log file on disk.

Can You Shrink the Log in SSMS Without SQL?

Yes. In Object Explorer, right-click the database, choose Tasks > Shrink > Files, set File type to Log, and either accept the currently allocated size or enter a smaller one. The dialog shows the current and minimum sizes, and the shrink runs the same DBCC command underneath. Menu labels can differ slightly between SQL Server Management Studio versions. The log backup in step 2 still has to happen separately, so most people find the script route faster.