Smart Systems for Home

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

Shrink a SQL Server Log File After a Full Backup

· 4 min read · Home Tech

In this article
  1. Shrink the Log in the Right Order
  2. Why the Full Backup Alone Does Not Free the Log
  3. What the Numbers Look Like in a Real Case
  4. Where Shrinking Goes Wrong
  5. Log Shrink Questions That Come Up

Yes, you can shrink a SQL Server transaction log after a full backup, but the full backup alone does not free the log. The log only truncates after a log backup (in FULL recovery) or a checkpoint (in SIMPLE recovery); then you run DBCC SHRINKFILE to release the freed space to the operating system.

Time 10 min · Steps 6 · Level Medium

Shrink the Log in the Right Order

  1. Check the recovery model. Run SELECT name, recovery_model_desc FROM sys.databases WHERE name = 'YourDB';. FULL means the log keeps every transaction until you back it up; SIMPLE means it frees space automatically at checkpoints.
  2. Take the full backup if you have not already. BACKUP DATABASE YourDB TO DISK = 'D:\Backup\YourDB.bak';. This protects you before touching the log, but it does not truncate it.
  3. Back up the log (FULL recovery only). BACKUP LOG YourDB TO DISK = 'D:\Backup\YourDB.trn';. This marks the inactive part of the log as reusable, which is what makes shrinking possible.
  4. Find the log file's logical name. SELECT name, size/128.0 AS SizeMB FROM sys.master_files WHERE database_id = DB_ID('YourDB') AND type_desc = 'LOG';. Note the name, usually something like YourDB_log.
  5. Shrink the file. USE YourDB; DBCC SHRINKFILE (YourDB_log, 256); where 256 is the target size in MB. Omit the number to shrink to the minimum possible.
  6. Verify the result. Re-run the query from step 4, or check the Files page in database properties, to confirm the log shrank to the size you asked for.

Why the Full Backup Alone Does Not Free the Log

Many people run a full backup, see the log file still huge on disk, and assume the backup failed. It did not. A full backup records the data files; it does not tell SQL Server to reuse the log space inside the .ldf file. In FULL recovery, only a transaction log backup (or a switch to SIMPLE recovery) marks that space reusable. In SIMPLE recovery, an automatic checkpoint does it, so a full backup followed by a checkpoint is often enough.

The shrink command then releases that reusable space to the operating system. Without the log backup or checkpoint first, DBCC SHRINKFILE either fails to shrink anything or shrinks only a little, because the space is still marked as in use.

What the Numbers Look Like in a Real Case

Say a database has 40 GB of data and a log that grew to 25 GB during a large one-off data load. After the load finishes, you back up the database, back up the log, and run DBCC SHRINKFILE (YourDB_log, 2048);. The log drops to about 2 GB on disk, releasing roughly 23 GB. If the routine log activity for this database only ever uses 1.5 GB between log backups, the file stays near 2 GB from then on.

If instead you shrink with no target size and the routine need is 1.5 GB, the file may shrink to under 1 GB, then grow again during the next busy period. Each auto-growth event takes time and fragments the file, so shrinking to a sensible floor is better than shrinking to the absolute minimum.

Where Shrinking Goes Wrong

The log grows right back

This is the classic outcome: shrink to 500 MB, and within days the file is 20 GB again. The shrink removed the symptom, not the cause. Find the cause first, usually log backups running too rarely, a long-running transaction, or an index rebuild in FULL recovery. Fix that, then shrink once.

Shrinking on a schedule

Do not put DBCC SHRINKFILE in a nightly job. The shrink-and-regrow cycle wastes I/O and fragments the log. Shrink is a one-time corrective step after an exceptional event, not maintenance.

Switching to SIMPLE recovery to shrink

Switching from FULL to SIMPLE truncates the log, but it breaks your log backup chain, which means point-in-time recovery is gone until you take a new full or differential backup. If you can tolerate that, fine; if the database matters, stay in FULL and back up the log instead.

Shrinking the wrong file

DBCC SHRINKFILE takes the logical file name, not the database name. Shrinking a data file (type 0) instead of the log (type 2) causes the same regrow problem for your data files and can slow queries while pages rearrange.

Log Shrink Questions That Come Up

Does a full backup truncate the log in SIMPLE recovery?

No, but in SIMPLE recovery the log truncates automatically at checkpoints, which happen frequently on a busy database. So by the time you run the shrink, the space is usually already reusable, which is why shrinking after a full backup often appears to work in SIMPLE mode.

Why does DBCC SHRINKFILE leave the log bigger than requested?

Usually because a log backup has not happened since the space was last used, or an active transaction still holds the tail of the log. Back up the log again and retry. If a virtual log file near the start of the file is still active, the shrink can only release space past the active portion.

How do I stop the log filling the disk in the first place?

In FULL recovery, schedule log backups often enough that the log never has to grow, for example every 15 minutes on a busy database. Also watch for long-running transactions with DBCC OPENTRAN;, since the log cannot truncate past the oldest active one.

Official source: Microsoft Support