How to Shrink a SQL Server Log File Safely
In this article
Back up the transaction log first, then run DBCC SHRINKFILE on the log file — shrinking without a log backup usually fails because the active part of the log sits at the end of the file. If the database is in Simple recovery, skip the backup and shrink directly.
Time 10 min · Steps 8 · Level Medium
The most common way this goes wrong: someone runs DBCC SHRINKFILE over and over, the file refuses to shrink, and they conclude the command is broken. It isn't — the log only shrinks back to the point of the last inactive virtual log file, so until you take a log backup (or the database is in Simple recovery), the tail of the file stays in use and nothing reclaims space. Do the backup first and the shrink usually works in one pass.
Check the Recovery Model Before You Touch the Log
The recovery model decides whether the log ever empties on its own. In Full recovery, the log grows until you back it up, so a huge log file almost always means log backups have never been scheduled. In Simple recovery, SQL Server reuses log space automatically and the file only stays big if a long-running transaction is holding it open.
Find your recovery model by right-clicking the database in Object Explorer and choosing Properties > Options — the Recovery model setting is near the top. Menu labels can differ slightly by version. If the database must support point-in-time restore (most production systems), keep Full recovery and fix the backup schedule instead of switching to Simple.
Shrink the Log File
- Back up the transaction log. Run
BACKUP LOG YourDatabase TO DISK = 'path\log.bak'. If the database is in Simple recovery, skip this step — a log backup will fail there. - Find the log file's logical name. Run
USE YourDatabase; SELECT name, size/128 AS size_mb FROM sys.database_files WHERE type_desc = 'LOG'. Thenamecolumn is what SHRINKFILE needs, andsize_mbshows the current size. - Check for long-running transactions. Run
DBCC OPENTRAN. If a transaction has been open for hours, shrink it after it commits or is killed, otherwise the log will not release its tail. - Run the shrink. Execute
USE YourDatabase; DBCC SHRINKFILE (LogFileName, 256)— this targets 256 MB. Replace the number with your target size in MB, but never below what a normal log cycle needs. - Re-check the size. Re-run the query from step 2 and compare
size_mbto the starting value. A successful shrink typically brings the file close to your target. - If it barely moved, back up and repeat. Take another log backup and run the same DBCC SHRINKFILE again. One or two extra rounds clears the remaining inactive space.
- Set a sensible growth cap. In Object Explorer, right-click the database > Properties > Files, click the ellipsis next to the log file's Autogrowth setting, and set a maximum file size so a runaway log cannot fill the disk.
- Schedule regular log backups. If the database is in Full recovery, back up the log on a fixed interval (commonly every 15–60 minutes). This is what stops the file ballooning again.
Log Won't Shrink: Symptom, Cause and Fix
| Symptom | Cause | Fix |
|---|---|---|
| SHRINKFILE runs but size barely changes | Active log at end of file; no recent log backup | Take a log backup, then shrink again |
| Log grows right back within days | Full recovery with no scheduled log backups | Schedule log backups every 15–60 minutes |
| Log huge in Simple recovery | Long-running transaction or bulk load holding space | Let the transaction finish, then shrink |
| Shrink reports it can't release space | Replication, mirroring or an availability group keeps log records | Fix or remove the paused replication/mirroring session |
| Error: file is not a log file | Wrong logical name passed to SHRINKFILE | Re-check the name with sys.database_files |
How to Tell It Worked, and What Not to Do Next
The shrink worked when the log file's size in sys.database_files (or the Initial Size column in database Properties > Files) has dropped to roughly your target, and the disk has the reclaimed free space. The database keeps running normally throughout — no restart is needed.
Resist the habit of shrinking the log on a schedule. Every shrink-then-grow cycle fragments the log and wastes time, so treat DBCC SHRINKFILE as a one-off cleanup after an incident, not maintenance. The permanent fix is the backup schedule from step 8, plus a growth cap so a single bad query can't consume the whole disk.
One more follow-up that comes up often: shrinking the log does not shrink the data file. The same command works for it — DBCC SHRINKFILE (DataFileName, target_mb) — but the same caution applies, and a data file that keeps growing points to missing index maintenance or unbounded table growth rather than a shrink problem.
Official source: Microsoft Support