How to Shrink a SQL Server Log File
In this article
Shrink a SQL Server transaction log with DBCC SHRINKFILE after first backing up the log, because the file only shrinks when the inactive part is freed. The exact command is DBCC SHRINKFILE (logical_name, target_size_in_MB) run against the database whose log you want to shrink.
Time 10 min · Steps 7 · Level Medium
The Shrink Steps
- Back up the log first. Run a transaction log backup (or switch the database to Simple recovery if you do not need point-in-time restores). Without one, SQL Server has nothing to mark as reusable and the shrink does almost nothing.
- Find the logical file name. Run SELECT name, size/128.0 AS Size_MB FROM sys.database_files to list the log's logical name and its current size in MB. You need the logical name, not the physical path.
- Check the recovery model. SELECT recovery_model_desc FROM sys.databases WHERE name = 'YourDB' tells you whether the log keeps every transaction (Full) or reuses space automatically (Simple).
- Free the inactive space. In Full or Bulk_Logged recovery, a log backup frees the inactive virtual log files. In Simple recovery, a CHECKPOINT usually does it.
- Run the shrink. USE YourDB; DBCC SHRINKFILE (YourDB_log, 1024) shrinks the log toward 1024 MB. SQL Server stops at the nearest virtual log file boundary, so the result is usually slightly above the target.
- Verify the new size. Re-run the query from step 2, or check the file size column in a query against sys.master_files, to confirm how much space was actually released.
- Fix the growth setting. Set a sensible auto-growth value (for example 512 MB increments rather than a percentage) so the log does not balloon and force you to shrink it again.
Why Logs Grow Big
The transaction log records every change so SQL Server can roll work back and, in Full recovery, restore to a point in time. It grows when log backups are never taken, when a long-running transaction holds old log records hostage, or when a one-off operation such as a huge index rebuild or bulk load writes far more than usual. In Full recovery the log is only truncated by log backups; if you take none, it grows without limit until the disk fills.
A common trigger is a database left in Full recovery with only daily full backups and no log backups in between. The log faithfully records every change since the last full backup and can easily reach tens of gigabytes on a busy database. The fix is a regular log backup schedule, typically every 15 minutes to an hour, not repeated shrinking.
What Shrinking Actually Does
Shrinking releases free space at the end of the file back to the operating system; it does not compress data and it does not touch the active part of the log. If the file is 50 GB and only 2 GB is in use, a shrink can return roughly 48 GB to the disk. If 45 GB is in use, the same shrink frees almost nothing, because SQL Server cannot move the active log records.
One misconception worth correcting: many people believe shrinking a log makes the database faster. The opposite is closer to the truth. Shrinking and regrowing a log repeatedly causes file-growth stalls and fragments the virtual log files, which slows writes. Shrink once to reclaim space after a known one-off event, then size the log to its normal working size and leave it alone.
Worked Example
Say a database's log grew to 40 GB during a large index rebuild, and after the operation plus a log backup, only 3 GB is in use. You want the log to sit at 8 GB so it has room for normal daily work:
First back up the log, then run USE SalesDB; DBCC SHRINKFILE (SalesDB_log, 8192). The 40 GB file shrinks to about 8 GB, releasing roughly 32 GB to the disk. Because virtual log files come in fixed chunks, the final size lands at the nearest boundary above 8 GB, so expect something like 8.1 GB rather than exactly 8.0. Then set auto-growth to 2048 MB per growth so day-to-day spikes grow the file in a few controlled steps instead of many tiny ones.
If the shrink returns only a small amount, the log's active portion sits near the end of the file. Back up the log again and repeat the shrink once or twice; each cycle moves the active section and lets SQL Server release more. If it still will not shrink, look for a long-running transaction with a query against sys.dm_tran_database_transactions or check whether replication or log shipping is holding log records.
Simple vs Full Recovery
If point-in-time recovery does not matter for a database, switching to Simple recovery is the cleanest way to stop runaway log growth. In Simple mode, SQL Server reuses log space automatically after each checkpoint, so the log stays near its working size with no log backups needed. The trade-off is real: you can only restore to your last full or differential backup, losing any changes made after it.
Keep Full recovery for databases where losing even a few minutes of data is unacceptable, and pair it with frequent log backups. A schedule of a full backup nightly plus log backups every 15 minutes means the log rarely grows beyond what 15 minutes of changes plus safety margin requires, and shrinking becomes a rare emergency tool rather than routine maintenance.
Shrink Follow-Up Questions
Can I shrink the log while the database is in use?
Yes, DBCC SHRINKFILE is an online operation, but it works best when the log is not actively growing. Run it during a quiet period, after a log backup, and expect it to run longer on a busy system.
What is the difference between shrinking the log and TRUNCATEONLY?
DBCC SHRINKFILE (name, TRUNCATEONLY) releases only the free space already at the tail of the file and does not move any pages. It is fast but frees less; a shrink with a target size can rearrange contents to free more.
Why does the log grow right back after I shrink it?
Because the workload that grew it is still running and the log has no headroom. Size it to the normal peak plus margin, keep log backups frequent, and it will stabilise instead of oscillating.
Is shrinking the data file the same command?
Yes, the same DBCC SHRINKFILE command works for data files, but shrinking data files causes index fragmentation, so rebuild indexes afterwards if you shrink one significantly.
Official source: Microsoft Support