Smart Systems for Home

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

Shrink a SQL Server Log File: Script and Steps

· 5 min read · Home Tech

In this article
  1. Run the Shrink Script Safely
  2. Why the Log Grew in the First Place
  3. A Worked Example With Real Numbers
  4. When Not to Shrink, and What to Do Instead
  5. Truncate the Log Only If You Accept the Trade
  6. Check the Result and Watch for Regrowth

Use DBCC SHRINKFILE with the logical file name of the log, after you back up the log or switch the database to SIMPLE recovery: DBCC SHRINKFILE (MyDB_log, 256); shrinks the log to about 256 MB. Shrinking the log without fixing what grew it first means it just grows right back.

Time 5 min · Steps 6 · Level Medium

Run the Shrink Script Safely

  1. Find the logical file name. Run SELECT name, size/128.0 AS SizeMB FROM sys.database_files WHERE type_desc = 'LOG'; — DBCC SHRINKFILE takes the logical name, not the physical .ldf path, and the logical name often does not match the database name.
  2. Free the log space first. In FULL recovery, run a log backup: BACKUP LOG MyDB TO DISK = 'D:\Backup\MyDB_log.trn';. In SIMPLE recovery, the inactive part clears automatically after a checkpoint — run CHECKPOINT; if needed.
  3. Shrink in small jumps. DBCC SHRINKFILE (MyDB_log, 256); targets 256 MB. Shrinking in steps of a few hundred MB avoids long single operations that block log writes.
  4. Check the result. Re-run the query from step 1, or DBCC SQLPERF(LOGSPACE); which shows the log size and the percentage actually used for every database.
  5. Stop the log growing back. If the shrink worked but the file balloons again within days, the cause is usually an uncommitted long transaction, log backups that are too infrequent, or a one-off index rebuild. Fix that, or the shrink was wasted work.
  6. Set a sensible growth increment. In database properties, set autogrowth to a fixed amount such as 512 MB or 1 GB instead of a percentage, so growth events are predictable and rare.

Why the Log Grew in the First Place

The transaction log records every change before it reaches the data file. In FULL recovery it keeps every record since the last log backup, so if backups run nightly but the log never gets backed up, it grows all day with nowhere to go. In SIMPLE recovery the log reuses its space constantly, so a huge log there almost always points to one long-running transaction holding the log active — a big index rebuild, a replication or mirror session that has fallen behind, or a session someone left open.

Shrinking does not change any of this. It only returns unused space to the operating system. If the log is 40 GB and 38 GB is genuinely in use, the shrink will barely move it, and that is the tool telling you the space is not free.

A Worked Example With Real Numbers

Say a reporting database has a log file at 12 GB, and DBCC SQLPERF(LOGSPACE); shows 6% used. That means about 0.7 GB is active and roughly 11.3 GB is reclaimable. After a log backup and DBCC SHRINKFILE (MyDB_log, 1024);, the file lands near 1 GB. If the log grows back to 12 GB within two weeks, the daily index maintenance is the culprit — schedule a log backup right after it, or move that maintenance to a window where the log can be backed up immediately, then shrink once more. A file that stays stable at 1 GB for a month tells you the fix worked.

A reasonable rule of thumb: size the log for your largest routine transaction plus headroom — often 1–2 GB for small databases and 5–10 GB for ones with nightly index rebuilds — and leave it there. A log that never needs to autogrow is a healthy log.

When Not to Shrink, and What to Do Instead

Recurring scheduled shrinks are a bad habit. Every shrink-then-grow cycle fragments the log's virtual log files, and growth events stall transactions while they wait for the disk allocation. Microsoft's own guidance treats shrink as a one-off operation for reclaiming space after an exceptional event — a one-time bulk load, a switch from FULL to SIMPLE recovery, or a log that ran away because backups failed for a week.

The better sequence for a chronically large log: fix the cause, take the log backup that clears the inactive space, shrink once to your target size, then leave the file alone. If disk pressure is the real problem, moving the log file to its own drive is the durable answer, and that is done with ALTER DATABASE ... MODIFY FILE plus a restart of the database, not with repeated shrinks.

Truncate the Log Only If You Accept the Trade

Switching to SIMPLE recovery with ALTER DATABASE MyDB SET RECOVERY SIMPLE; clears the log automatically and is a legitimate choice for dev databases or read-only reporting copies. But it breaks point-in-time recovery: with SIMPLE recovery you can only restore to your last full or differential backup. For a production database in FULL recovery, the right move is more frequent log backups — every 15 minutes is a common starting point — not a recovery model change made just to shrink a file.

Also note that BACKUP LOG ... WITH TRUNCATE_ONLY no longer exists in current SQL Server versions; it was removed because it broke the log backup chain. If an old script references it, that script needs rewriting, not just re-running.

Check the Result and Watch for Regrowth

After shrinking, verify three things: the file size in sys.database_files, the used percentage with DBCC SQLPERF(LOGSPACE);, and the autogrowth setting in the database properties under Files (menu labels can differ slightly by version or tool). Then check the default trace or the SQL Server error log for growth events over the following days — if the log autogrew twice since the shrink, the underlying cause is still live. A log that holds steady at its new size for a couple of normal workload cycles, including your nightly maintenance window, is the real confirmation that the job is done.

Official source: Microsoft Support