Smart Systems for Home

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

How to Shrink an MS SQL Log File (DBCC SHRINKFILE)

· 4 min read · Home Tech

In this article
  1. The DBCC SHRINKFILE steps for a transaction log
  2. Why the log refuses to shrink until you back it up
  3. A worked shrink with real numbers
  4. Why shrinking the log repeatedly works against you
  5. Commands you'll type, in order
  6. What to do if the log grows straight back

Run DBCC SHRINKFILE with the log file's logical name and a target size in MB, after first truncating the log with a backup or a checkpoint in SIMPLE recovery. The command is: DBCC SHRINKFILE (LogicalName, target_MB).

Time 10 min · Steps 6 · Level Medium

The DBCC SHRINKFILE steps for a transaction log

  1. Find the log's logical name. Run SELECT name, size/128 AS SizeMB FROM sys.database_files WHERE type_desc = 'LOG' to get the logical name and current size in MB.
  2. Check the recovery model. Run SELECT recovery_model_desc FROM sys.databases WHERE name = DB_NAME(). FULL or BULK_LOGGED needs a log backup first; SIMPLE does not.
  3. Truncate the inactive part of the log. In FULL/BULK_LOGGED, back up the log (BACKUP LOG MyDB TO DISK = '...'). In SIMPLE, a checkpoint clears it automatically.
  4. Shrink the file. Run DBCC SHRINKFILE (MyDB_log, 256) to target 256 MB. Omit the target size to shrink to the minimum possible.
  5. Check the result. Re-run the query from step 1, or look at the file size in SSMS under database properties.
  6. Stop the regrowth cycle. If the log ballooned from autogrowth, cap it with ALTER DATABASE MyDB MODIFY FILE (NAME = MyDB_log, MAXSIZE = 512MB) so it cannot silently grow back.

Why the log refuses to shrink until you back it up

The most common reason DBCC SHRINKFILE returns instantly with no change is that the log's virtual log files are all still active. SQL Server can only remove the inactive portion of the log, and in FULL or BULK_LOGGED recovery the log stays active until you back it up. A checkpoint alone is not enough in those models. Back up the log, then shrink, and the file will usually reduce on the first or second attempt.

Two other frequent blockers: a long-running open transaction holds the oldest log records, so check DBCC OPENTRAN if shrinking fails after a backup; and log replication, mirroring or an availability group that is behind keeps the log active until the secondary catches up.

A worked shrink with real numbers

Say a reporting database in FULL recovery has a log that grew to 8 GB during a large index rebuild, and the target is 512 MB. After a log backup, the query in step 1 shows MyDB_log at 8192 MB. Run DBCC SHRINKFILE (MyDB_log, 512). SQL Server removes inactive virtual log files from the end of the file, and the log lands at or near 512 MB. If it stops at, for example, 2048 MB, one active virtual log file is near the end of the file; back up the log again and re-run the shrink, and it typically reaches the target on the second pass.

If you shrink with no target size at all, the file goes to the minimum SQL Server can achieve at that moment, which may be far smaller than the log needs for routine work, so a target size is usually the safer call.

Why shrinking the log repeatedly works against you

Shrinking is a one-off cleanup, not maintenance. Every shrink followed by regrowth fragments the log's virtual log files into thousands of small pieces, which slows log writes and log backups. If the log grows back to the same size every week, the fix is to find the cause, not to shrink harder: run a big transaction in smaller batches, schedule log backups more often in FULL recovery, or switch a database that does not need point-in-time restore to SIMPLE recovery.

One misconception worth correcting: shrinking the log does not back up or clear anything by itself. DBCC SHRINKFILE only returns unused space to the operating system; the truncation that makes space unused comes from the log backup or checkpoint beforehand.

Commands you'll type, in order

The full sequence for a database called Sales with a log named Sales_log, targeting 256 MB:

USE master;
BACKUP LOG Sales TO DISK = 'C:\Backup\Sales_log.trn';
USE Sales;
DBCC SHRINKFILE (Sales_log, 256);
SELECT name, size/128 AS SizeMB FROM sys.database_files WHERE type_desc = 'LOG';

Menu names and dialog labels in SSMS can differ slightly by version, but the T-SQL above is stable across versions. If you prefer the GUI, right-click the database, choose Tasks > Shrink > Files, set File type to Log, and enter the target size, though the script gives you the same result with a record of what ran.

What to do if the log grows straight back

Check three things in order. First, the log backup schedule: in FULL recovery with no log backups at all, the log grows without limit, so add regular log backups between full backups. Second, long transactions: a single overnight batch that runs for hours holds the log the whole time, so break it into chunks that commit. Third, the autogrowth setting: growth in small increments, such as 10 percent, creates many virtual log files, so set a fixed growth increment of a few hundred MB instead. Once those are fixed, one final shrink brings the file to its working size and it stays there.

Official source: Microsoft Support