SQL Command to Shrink a Log File (DBCC SHRINKFILE)
In this article
Use DBCC SHRINKFILE (logical_name, target_size_in_MB) to shrink a SQL Server transaction log file. Back up the log first, or the shrink will do almost nothing because the active portion of the log cannot be released.
Time 10 min · Steps 6 · Level Medium
Shrinking the Log in 6 Steps
- Find the logical file name. Run
SELECT name, size/128.0 AS size_MB FROM sys.database_files WHERE type_desc = 'LOG';Thenamecolumn is the logical name DBCC SHRINKFILE needs, which is often different from the physical file name on disk. - Check the recovery model. Run
SELECT recovery_model_desc FROM sys.databases WHERE name = DB_NAME();In FULL recovery the log only clears after a log backup; in SIMPLE it clears automatically at checkpoints. - Back up the transaction log. In FULL (or BULK_LOGGED) recovery run
BACKUP LOG YourDatabase TO DISK = 'path\log.bak';This marks the log inactive so the shrink has space to reclaim. Skip this only if you are in SIMPLE recovery. - Run the shrink command.
USE YourDatabase; DBCC SHRINKFILE (YourDatabase_log, 256);The second argument is the target size in megabytes, so 256 aims for a 256 MB file. Omit the number to shrink to the smallest possible size, which is rarely what you want. - Check the result. Re-run the query from step 1, or look at the file size in the database properties in SQL Server Management Studio. If the file barely shrank, the log could not be truncated - see the fixes below.
- Verify the database is healthy. Run a normal full backup so your recovery chain is intact, especially if you switched recovery models to make the shrink work.
Why the Log Grows in the First Place
The transaction log records every change before it reaches the data files, so it can grow large after index rebuilds, bulk loads, long-running transactions or a stretch of time with no log backups. Growth itself is normal and the file is reused in a circular fashion once portions are marked inactive. The file only stays huge when nothing ever truncates it - and a file that has grown does not shrink on its own.
Recovery Model Decides What You Must Do First
The single biggest reason a shrink fails is that the log has never been backed up, so every part of it is still needed. What you do before DBCC SHRINKFILE depends on the recovery model:
| Recovery model | What clears the log | Pre-shrink action |
|---|---|---|
| SIMPLE | Automatic checkpoints | Just run DBCC SHRINKFILE; a backup is not required |
| FULL | Log backups only | Run BACKUP LOG first, then shrink |
| BULK_LOGGED | Log backups (with limits after bulk operations) | Run BACKUP LOG first, then shrink |
Rule of thumb: if you are in FULL recovery and have never taken a log backup, the log will grow until the disk is full. Scheduling regular log backups is the real fix; the shrink is just the cleanup.
When the Shrink Refuses to Release Space
If DBCC SHRINKFILE returns almost immediately with no size change, the end of the logical log is sitting at the start of the file, so there is nothing to trim. The standard workaround is to shrink repeatedly in a loop, or to checkpoint and back up the log between attempts. A long-running transaction also pins the log: DBCC OPENTRAN shows the oldest active transaction, and the log cannot truncate past it. Replication, database mirroring or an availability group that is behind can hold the log the same way - the log stays large until the secondary catches up.
A last-resort sequence, safe only when you accept losing point-in-time recovery, is to switch to SIMPLE recovery, run BACKUP LOG YourDatabase WITH TRUNCATE_ONLY - removed in newer versions, so the model switch itself does the truncation - then shrink, then switch back to FULL and immediately take a full backup. Breaking the log backup chain this way means you can only restore to the new full backup, so use it for a dev database or a genuine emergency, not as routine maintenance.
Shrink Once, Then Stop It Growing Again
Repeated shrinking is a habit worth breaking. Every shrink-and-regrow cycle fragments the virtual log files internally and adds auto-growth stalls while queries wait. After one shrink to a sensible size, set an explicit file size so growth is deliberate: ALTER DATABASE YourDatabase MODIFY FILE (NAME = YourDatabase_log, SIZE = 512MB, FILEGROWTH = 128MB); A fixed growth increment in megabytes beats a percentage, because percentage growth gets slower each time the file gets bigger.
If the log keeps ballooning, the cause is usually one of three things: missing log backups in FULL recovery, a long-running transaction, or an index maintenance job that runs in one giant transaction. Fix the cause and the log will settle at a steady working size on its own.
Questions That Come Up With DBCC SHRINKFILE
Does shrinking the log lose data?
No. Shrinking releases only inactive, already-truncated space in the log file. Committed transactions are safe in the data files. What you can lose is recovery history: if you truncate the log by switching to SIMPLE recovery, you give up the ability to restore to a point in time before your next full backup.
Should the target size be the minimum possible?
No. Aim for a size the log actually needs in normal operation - roughly the space it uses between your regular log backups. If you shrink to the absolute minimum, the file immediately starts auto-growing again, which is slower and fragments the log.
Is DBCC SHRINKFILE the same as shrinking the whole database?
DBCC SHRINKFILE targets one file and is the right tool for a bloated log. DBCC SHRINKDATABASE shrinks every file in the database, including data files, which causes data-file fragmentation for little benefit. For a log problem, use SHRINKFILE with the log's logical name.
Does this work on other databases besides SQL Server?
The syntax here is SQL Server (T-SQL). Other engines handle logs differently - in Oracle you manage redo log groups rather than shrinking files, and in PostgreSQL the write-ahead log recycles automatically. If you are not on SQL Server, search for your engine's own log management commands.
Official source: Microsoft Support