Smart Systems for Home

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

How to Shrink a Transaction Log File in SQL Server

· 5 min read · Home Tech

In this article
  1. Shrink the Log File, Step by Step
  2. Check the Recovery Model Before You Touch the File
  3. What the Log File Will Not Shrink Means
  4. Before You Start: A Short Checklist
  5. Four Mistakes and What Each One Causes
  6. Picking a Target Size That Sticks
  7. Checking the Result and When to Leave the File Alone

Back up the transaction log first, then run DBCC SHRINKFILE on the log file — that is the standard sequence, and skipping the backup is the most common reason the file refuses to shrink. The method differs when the database is in Simple recovery model, where no log backup is needed.

Time 10 min · Steps 7 · Level Medium

Shrink the Log File, Step by Step

  1. Check the recovery model. Run SELECT name, recovery_model_desc FROM sys.databases WHERE name = 'YourDB';. If it says FULL, the log must be backed up before it can shrink. If it says SIMPLE, go straight to the shrink step.
  2. Find the log file's logical name and size. Run USE YourDB; SELECT name, size/128.0 AS SizeMB, type_desc FROM sys.database_files; and note the row where type_desc is LOG.
  3. Back up the transaction log. For a FULL recovery database run BACKUP LOG YourDB TO DISK = 'D:\Backup\YourDB_log.trn';. Without this, the active portion of the log stays in place and the shrink does almost nothing.
  4. Shrink the file by name. Run DBCC SHRINKFILE (YourDB_log, 256); where 256 is the target size in MB. Replace the logical name you found in step 2 and pick a target that leaves room for normal growth.
  5. Check the result. Re-run the query from step 2 and compare the SizeMB value. If it barely moved, the log's active portion is still sitting at the end of the file.
  6. Retry once if needed. Take one more log backup, then run the same DBCC SHRINKFILE command again. A second pass after a fresh backup often reclaims the space the first pass could not.
  7. Verify the database is healthy. Run DBCC CHECKDB ('YourDB'); on a quiet window to confirm nothing about the database itself needs attention before you walk away.

Check the Recovery Model Before You Touch the File

The recovery model decides whether the log can ever be emptied by a backup. In FULL recovery, every change is kept in the log until a log backup marks it reusable, so an un-backed-up log grows and grows. In SIMPLE recovery, SQL Server reuses log space automatically after each checkpoint, and a log backup is not even possible. If a database is in FULL but nobody takes log backups, that is the real cause of the oversized file — either schedule log backups or switch to SIMPLE with ALTER DATABASE YourDB SET RECOVERY SIMPLE; if you do not need point-in-time restore.

What the Log File Will Not Shrink Means

A shrink that frees nothing is a symptom, not a failure of the command. The log is a ring of records, and the active part — the part SQL Server is still writing — can sit at the physical end of the file. Everything before it is free, but the file cannot shrink past the active part.

SymptomLikely causeFix
Shrink frees almost nothingLog never backed up (FULL model)Take a log backup, then shrink again
Log grows back within daysTarget size too small, or a long-running transactionPick a realistic target; check sys.dm_tran_active_transactions for old open transactions
Cannot shrink below a sizeActive virtual log file at the end of the fileAnother log backup plus a second shrink attempt usually moves it
Shrink command errors on the nameWrong logical name usedUse the name from sys.database_files, not the file name on disk
Log huge after a bulk load or index rebuildOne-off operation blew it upBack up the log, shrink once, then leave it alone

Before You Start: A Short Checklist

  • Confirm you have a recent full database backup, since shrinking is safe but you should never do log surgery without one on hand.
  • Know the recovery model and whether log backups are scheduled — ask whoever runs the backups if that is not you.
  • Run the shrink during a quiet period, not mid-batch or during heavy writes.
  • Decide a sensible target size before running the command rather than shrinking to the minimum.

Four Mistakes and What Each One Causes

Shrinking to the minimum size. DBCC SHRINKFILE (log, 1) squeezes the file to almost nothing, then it immediately regrows in small increments, which fragments the log and slows every write. Give it room to live.

Shrinking on a schedule. A nightly shrink-and-regrow cycle is pure churn: growth events cost time and the file ends up the same size anyway. Shrink once after a known one-off event, not routinely.

Using TRUNCATEONLY on the log. TRUNCATEONLY only frees space at the end of the file, which for a log is usually exactly where the active part sits, so it typically frees nothing. Give an explicit target size instead.

Switching to SIMPLE on a database that needs point-in-time restore. This breaks the log backup chain, and after that you can only restore to the last full or differential backup. Confirm with whoever owns the restore plan before changing the model.

Picking a Target Size That Sticks

The right target is a bit larger than the log's normal working size, so it never has to grow again. Look at how large the file gets between routine log backups — if it sits around 200 MB after normal operation, a target of 256 MB is sensible. A log that has to grow during a heavy transaction stalls that transaction while SQL Server zero-initialises the new space, so a little headroom is cheap insurance. Also set the file's autogrowth to a fixed increment in MB rather than a percentage, so growth is predictable.

Checking the Result and When to Leave the File Alone

After the shrink, re-run the sys.database_files query and confirm the size dropped to your target. Then watch it for a week: if it stays flat, you are done. If it climbs back, the fix is not another shrink — it is either more frequent log backups, a larger target, or tracking down the long-running transaction holding log space. A log file that is big but stable is not a problem; it is just a file that has stopped growing. Repeated shrinking of a healthy, stable log buys you nothing and costs write performance, so once the one-off bloat is gone, let it be.

Official source: Microsoft Support