Smart Systems for Home

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

SQL Server Shrink Log File in Simple Recovery Mode

· 5 min read · Home Tech

In this article
  1. How Do You Shrink the Log File?
  2. Why Does the Log Grow Big in Simple Mode?
  3. What Target Size Should You Pick?
  4. Why Does the Shrink Only Free Part of the Space?
  5. When Should You Not Do This?
  6. Shrinking the Log Without Losing Full Recovery

In simple recovery mode, switch the database to simple recovery (if it is not already), run a checkpoint or back up the database, then run DBCC SHRINKFILE on the log file. The log usually shrinks right away because simple mode reclaims log space automatically after each checkpoint.

Time 5 min · Steps 7 · Level Easy

First, check that the database really is in simple recovery: run SELECT recovery_model_desc FROM sys.databases WHERE name = 'YourDB'. If it already returns SIMPLE, the log is likely large because of a one-off operation (an index rebuild, a bulk load), and a shrink will stick. If it returns FULL, shrinking alone will not help for long, because an unbroken chain of log backups keeps every section of the log in use.

How Do You Shrink the Log File?

  1. Check the recovery model. Run SELECT name, recovery_model_desc FROM sys.databases and confirm the target database shows SIMPLE.
  2. Switch to simple mode if needed. Run ALTER DATABASE YourDB SET RECOVERY SIMPLE;. Only do this if you do not need point-in-time restore for that database.
  3. Force the log to be reusable. Run USE YourDB; CHECKPOINT; so active portions of the log are marked reusable.
  4. Find the log file's logical name. Run USE YourDB; SELECT name, size/128 AS SizeMB FROM sys.database_files WHERE type_desc = 'LOG';
  5. Shrink the log. Run USE YourDB; DBCC SHRINKFILE (YourDB_log, 64); where the number is the target size in MB.
  6. Check the result. Re-run the query from step 4, or look at the file size in Object Explorer under Databases > YourDB (right-click > Tasks > Shrink > Files shows current and proposed sizes).
  7. Return to full recovery if you switched. Run ALTER DATABASE YourDB SET RECOVERY FULL; and immediately take a full or differential backup, because the backup chain is broken by the switch.

Why Does the Log Grow Big in Simple Mode?

Simple recovery truncates the log at every checkpoint, but it cannot shrink the physical file on disk. The file only ever grows; it never gets smaller on its own. So one large index rebuild, one huge DELETE, or a long-running transaction can push the file to several gigabytes, and it stays that size forever until you shrink it. A long-running open transaction is the usual culprit when a shrink returns only a partial reduction: the active part of the log cannot move, so the file can only shrink down to the active portion.

The other common cause is waiting for log backup. That applies to full recovery, not simple, but many people believe they are in simple mode when the database is actually in full. Always verify with the query above rather than trusting the setup.

What Target Size Should You Pick?

Shrink to a size the log actually needs, not to the minimum. If you shrink a log to 1 MB and the next index rebuild needs 5 GB, SQL Server will grow the file again in small increments, which is slow and can fragment the virtual log files. A practical rule of thumb: shrink to roughly the log size your database holds between routine maintenance operations, then leave it alone. For a small database, 64 MB to 512 MB is a common landing spot; for one with nightly index maintenance, the log may legitimately need to stay several GB.

Also set the file's autogrowth to a fixed increment (for example 256 MB) rather than a percentage, so any future growth happens in a few large steps instead of many tiny ones. You can set this in Object Explorer: right-click the database > Properties > Files, then edit the Autogrowth column. Exact labels can differ slightly by version.

Why Does the Shrink Only Free Part of the Space?

DBCC SHRINKFILE can only remove the inactive, unused part of the log, and only from the end of the file. If the result shows the file shrank but not to your target, one of these is usually holding space:

  • An open long-running transaction. Check with DBCC OPENTRAN in the database, then deal with that transaction and shrink again.
  • A replication, mirroring or log-reading process that has not yet read the log. If replication is configured and the reader is behind, the log cannot truncate past what it still needs.
  • An active backup still running. Wait for it to finish, checkpoint, then shrink again.
  • You ran the shrink from master without a USE YourDB first, so it targeted nothing.

In most cases the fix is: resolve the blocker, run CHECKPOINT, then shrink again. Repeatedly shrinking and letting the file regrow is worse than leaving it large.

When Should You Not Do This?

Do not switch a production database to simple recovery just to shrink the log if you depend on point-in-time restore. In full recovery, the correct fix is to take regular transaction log backups (this is what marks log space reusable), and then shrink once, as a cleanup of a historical high-water mark. Signs that simple mode is the right setting for you: the database is a dev or test copy, log backups are never taken, or the data can be restored from the last full backup alone. Signs you should stay in full: you take log backups, or the database must be recoverable to a specific minute.

Signs the shrink worked: the .ldf file size drops in File Explorer or in the Shrink Files dialog, and the query in step 4 reports the smaller size in MB. If the size did not change at all, work through the blockers list above before trying again.

Shrinking the Log Without Losing Full Recovery

One follow-up worth knowing: you can shrink a log while staying in full recovery. Take a log backup (this truncates the inactive part of the log), then run the same DBCC SHRINKFILE command. The switch to simple mode is only a shortcut, not a requirement, and it costs you your restore chain. If you have never taken a log backup on a full-recovery database, that alone explains a log that never stops growing: the log is waiting for its first backup to mark space as reusable.

Official source: Microsoft Support