Shrink a SQL Server Log File with a Query
In this article
Yes — you shrink a SQL Server transaction log with the DBCC SHRINKFILE command, targeting the logical log file name, usually after backing up the log so the inactive portion is released. The basic query is DBCC SHRINKFILE (N'YourDatabase_log', 1024), where 1024 is the target size in MB.
Time 10 min · Steps 7 · Level Medium
Why the Log Refuses to Shrink Until You Free Space
DBCC SHRINKFILE only removes the inactive part of the log — the part SQL Server has already backed up or truncated and marked reusable. If the log is 40 GB and almost all of it is active (uncommitted transactions, an unbacked-up log, or replication still holding it), the command returns quickly and the file barely changes. So the real sequence is: free the inactive space first, then shrink. That is why a shrink that "doesn't work" is almost never a syntax problem; it is a recovery-model or open-transaction problem.
The Query Itself, Step by Step
- Find the log file's logical name. Run USE YourDatabase; SELECT name, size/128.0 AS SizeMB FROM sys.database_files; The row with type_desc = 'LOG' gives you the exact logical name you must pass to DBCC SHRINKFILE — it is often not what you guessed.
- Back up the log to mark space reusable. In full or bulk-logged recovery, run BACKUP LOG YourDatabase TO DISK = 'path\log.bak'; In simple recovery, skip this — the log truncates automatically at checkpoints.
- Check for blockers. Run DBCC OPENTRAN and SELECT log_reuse_wait_desc FROM sys.databases WHERE name = 'YourDatabase'; If the wait is anything other than NOTHING, fix that cause first (a long-running transaction, an unbacked-up log, or mirroring/replication).
- Run the shrink. DBCC SHRINKFILE (N'YourDatabase_log', 1024); The second argument is the target size in megabytes. Omit it to shrink to the minimum SQL Server can reach.
- Repeat the log backup if the first shrink stalls. A log shrinks in stages: it truncates, shrinks what it can, truncates again. Running BACKUP LOG then DBCC SHRINKFILE two or three times in a loop often gets the file down in one sitting.
- Verify the result. Re-run the sys.database_files query from step 1 and compare SizeMB. Also check the used percentage with DBCC SQLPERF (LOGSPACE);
- Reset the log's auto-growth. After a big shrink, set a sensible growth increment (for example 512 MB fixed growth instead of 10 percent) so the file does not fragment from thousands of tiny growth events.
Simple vs Full Recovery: Which Path Applies to You
The recovery model decides what you do before shrinking, so check it first with SELECT recovery_model_desc FROM sys.databases WHERE name = 'YourDatabase';
| Recovery model | Before shrinking | Typical result |
|---|---|---|
| Simple | Nothing — log truncates at checkpoint; just run DBCC CHECKPOINT then shrink | Shrinks well unless a transaction is still open |
| Full | BACKUP LOG required, or the log stays 100% active | Shrinks only after a log backup; repeat backup + shrink if needed |
| Bulk-logged | Log backup, same as full | Same as full, but bulk operations can hold large chunks |
Rule of thumb to remember: in simple recovery the log shrinks itself once transactions close; in full recovery you must back up the log before SQL Server will let the space go.
When the Shrink Runs but the File Barely Moves
The most common cause is log_reuse_wait. If DBCC OPENTRAN shows an old transaction, that transaction's log records cannot be truncated — kill or wait out the transaction, then back up and shrink again. If the wait is LOG_BACKUP, you simply have not taken a log backup yet. If it is ACTIVE_TRANSACTION or REPLICATION, address that first; shrinking before that is wasted effort. A virtual log file at the physical end of the file can also block shrinking even when most of the log is free — the backup-and-shrink loop from step 5 usually walks SQL Server past it.
One more trap: shrinking a log that is still growing because of a runaway process (index rebuilds, bulk loads, log backups never scheduled) just means it grows right back. Fix the growth driver, or the shrink buys you hours, not a solution.
Shrinking Repeatedly Is a Symptom Fix, Not a Plan
A one-off shrink after an unusual event — a giant one-time load, a switch to simple recovery, or a database moved to a smaller server — is a reasonable cleanup. Scheduling a weekly shrink is not: each shrink-and-regrow cycle creates hundreds of small virtual log files, which slows log reads and can make future recovery longer. If your log keeps ballooning, the durable fixes are scheduling regular log backups in full recovery, or moving to simple recovery for databases where point-in-time restore is not needed. Shrink once, then leave the file at a size that fits normal operation.
Checking Your Result the Right Way
Two numbers tell you everything. DBCC SQLPERF (LOGSPACE) lists every database's log size and the percentage actually used — a healthy log after cleanup typically sits well under 50% used. And sys.database_files with size/128.0 shows the physical file size in MB, which is what the shrink actually changed. If LOGSPACE shows low usage but the file is still large, run the backup-and-shrink loop again; if usage is still high, something is holding the log and step 3's log_reuse_wait_desc will name it.
Official source: Microsoft Support