How to Shrink a SQL Server Transaction Log File
In this article
Run DBCC SHRINKFILE on the log file after backing it up, because a transaction log only shrinks once its inactive portion has been truncated or backed up. In full recovery mode, back up the log first; in simple recovery mode, the log clears on its own and you can shrink right away.
Time 10 min · Steps 6 · Level Medium
The Shrink Sequence That Works
- Check the recovery model. Run
SELECT name, recovery_model_desc FROM sys.databases;so you know whether the log is waiting on a backup (full) or clears itself (simple). - Back up the log if you are in full recovery. Use
BACKUP LOG YourDatabase TO DISK = 'path\log.bak'. Without this step, SQL Server has almost nothing inactive to free, and the shrink will barely move the file. - Find the log's logical name and size. Run
DBCC SQLPERF(LOGSPACE);or querysys.database_filesfor the file with type_desc = 'LOG'. - Shrink to a target size. Run
USE YourDatabase; DBCC SHRINKFILE (LogicalLogName, 512);where 512 is the target in megabytes. Leave off the number to shrink to the minimum possible. - Check the result. Query
sys.database_filesagain and compare size (reported in 8 KB pages, so divide by 128 to get megabytes). - Regrow deliberately if needed. If the log was huge for a reason, set a sensible autogrowth increment (for example 512 MB, not 10 percent) so it does not fragment itself regrowing in tiny chunks.
Why the Log Refuses to Shrink
The single most common reason a shrink command does nothing is that the log is not actually reusable yet. A transaction log is a ring of virtual log files, and SQL Server can only release space back to the operating system from the part that is inactive. An active transaction, a long-running open transaction, transactional replication or mirroring that has not been delivered, or a database in full recovery that has never had a log backup all keep the active portion pinned near the end of the file.
Run DBCC LOGINFO('YourDatabase'); to see the virtual log files. Status 2 means the segment is active. If the last segment has status 2, the tail of the file is in use and shrinking will truncate nothing meaningful. Fix the cause: back up the log, kill the long-running transaction if it is safe to do so, or wait for replication to catch up, then shrink again.
Simple Recovery Makes It Almost Easy
If the database does not need point-in-time recovery, switching to simple recovery changes the whole picture. Run ALTER DATABASE YourDatabase SET RECOVERY SIMPLE;, then CHECKPOINT;, then shrink the file. The log truncates automatically at checkpoints, so the shrink has free space to reclaim. This is the standard approach for development and test databases. Be aware that switching from full to simple recovery breaks the log backup chain, so if you later switch back to full, take a full or differential backup to restart it.
Never switch a production database that relies on point-in-time restore to simple recovery just to reclaim disk space. That trade gives up the ability to restore to a moment between full backups, which is exactly what full recovery exists to provide.
What Shrinking Actually Does to the File
Shrinking removes the inactive virtual log files from the end of the file and releases that space to the operating system. It does not touch the data file unless you run the shrink against it, and it does not delete committed transactions from the log's logical sequence; those are already marked reusable. The command can run while the database is online, though heavy write activity during the shrink will slow it and can cause it to stop early, leaving the file larger than requested.
A rule of thumb worth keeping: shrink the log once, after an exceptional event, not on a schedule. A log that grew to 40 GB probably grew because something needed 40 GB, such as a one-off index rebuild, an uncommitted batch job, or missing log backups. If you shrink it to 1 GB and the same workload returns, the file grows again, and each regrowth is a hit to performance plus physical file fragmentation on the disk.
The Misconception That Keeps Logs Huge
The most persistent misconception is that the transaction log grows because of the data in the tables. It does not. The log grows from write activity, open transactions, and, above all, from full recovery without log backups. A database with 2 GB of data and no log backups can easily carry a 50 GB log file, and shrinking it every week just hides the symptom. In full recovery, take regular log backups (every 15 minutes to an hour is a common production cadence) and the log stays small on its own, because each backup marks the log space reusable.
One more trap: DBCC SHRINKDATABASE shrinks every file, including data files, and shrinking data files causes index fragmentation that you then have to rebuild. Target the log file by name with DBCC SHRINKFILE instead, and rebuild indexes only if you have already shrunk a data file.
Checking the Result and Setting Growth Sensibly
After shrinking, confirm with SELECT name, size/128.0 AS size_mb, max_size, growth FROM sys.database_files;. If the log is still larger than the target, look again at DBCC LOGINFO for a status 2 segment near the end, or check sys.databases for log_reuse_wait_desc, which names the exact reason space is being held, such as ACTIVE_TRANSACTION, LOG_BACKUP, or REPLICATION. That column is the fastest diagnostic on the whole topic: it tells you what to fix before any shrink can work.
Finally, pre-size the log to a realistic working size rather than letting it autogrow in a pinch. A fixed initial size with a modest autogrowth increment in megabytes gives predictable performance, and a log that is the right size for the workload never needs shrinking at all.
Official source: Microsoft Support