Fix SQL Server Transaction Log Full โ Free Up Log Space Fast
When your SQL Server transaction log fills up, every write operation fails immediately โ inserts, updates, deletes, and even schema changes. This guide explains exactly why it happens and gives you the fastest, safest path back to a working database.
โ This is the most common SQL Server blocker in production environments.
What Causes the Transaction Log to Fill Up?
The SQL Server transaction log records every modification made to a database โ every INSERT, UPDATE, DELETE, index rebuild, and schema change. The log is designed to be reused in a circular fashion: once SQL Server confirms it no longer needs a portion of the log for recovery or replication, it marks that portion as inactive and reuses it.
The log becomes full and triggers Error 9002 when SQL Server cannot reuse the inactive portions fast enough to keep up with incoming writes. The exact reason why log space can't be reused is shown in the log_reuse_wait_desc column of sys.databases:
-- Find out WHY the log is full (run immediately when you hit Error 9002) SELECT name AS DatabaseName, log_reuse_wait_desc AS WhyLogCannotBeReused, CAST(log_size_mb AS decimal(10,1)) AS LogSizeMB, CAST(log_used_mb AS decimal(10,1)) AS LogUsedMB FROM sys.dm_db_log_space_usage CROSS JOIN (SELECT SUM(size) * 8.0 / 1024 AS log_size_mb, SUM(FILEPROPERTY(name, 'SpaceUsed')) * 8.0 / 1024 AS log_used_mb FROM sys.database_files WHERE type_desc = 'LOG') lf JOIN sys.databases ON database_id = DB_ID();
LOG_BACKUP โ No Log Backups Scheduled
The most common cause. In FULL recovery model, SQL Server never reuses log space until a transaction log backup is taken. Without a scheduled backup job, the log grows indefinitely until it fills the drive.
ACTIVE_TRANSACTION โ Long-Running Transaction
A transaction that started hours (or days) ago and has not yet committed or rolled back holds the log open from its start point. Nothing before that point can be freed. Identify and kill or commit the blocking transaction.
REPLICATION โ Log Reader Agent Lag
In transactional replication, the Log Reader Agent must read and deliver every log record to the distribution database before that log space can be reused. If the agent is behind, the log fills up on the publisher.
INDEX_BUILD โ Large Index Rebuild
Rebuilding a large non-partitioned index in FULL recovery model is fully logged and can generate enormous amounts of log data. Running REBUILD operations offline or partitioned can significantly reduce log usage.
Immediate Fix: Log Backup + DBCC SHRINKFILE
If log_reuse_wait_desc shows LOG_BACKUP, this two-step process will free log space immediately without any data loss:
Step 1 โ Take a Transaction Log Backup
A log backup truncates the inactive portion of the log โ it marks that space as reusable. You must have a full backup on file before running a log backup. Back up to a local path or network share:
-- Transaction log backup โ frees inactive log space BACKUP LOG [YourDatabaseName] TO DISK = N'D:\Backups\YourDatabase_log_20260701.bak' WITH COMPRESSION, STATS = 10; GO -- Check remaining log space after backup DBCC SQLPERF(LOGSPACE);
After running the backup, SQL Server internally marks inactive VLFs (Virtual Log Files) as reusable. The log file size on disk won't shrink yet โ for that, proceed to Step 2.
Step 2 โ Shrink the Log File with DBCC SHRINKFILE
Backing up the log makes space reusable, but does not physically reduce the .LDF file size. To return disk space to the OS, run DBCC SHRINKFILE. Replace YourDatabase_log with your actual log file logical name (found in SSMS under Database โ Properties โ Files):
-- Find the logical name of your log file SELECT name, physical_name, type_desc FROM sys.database_files WHERE type_desc = 'LOG'; -- Shrink log file to 256 MB (adjust target size as needed) USE [YourDatabaseName]; GO DBCC SHRINKFILE (N'YourDatabase_log', 256); GO -- Verify new size DBCC SQLPERF(LOGSPACE);
Running DBCC SHRINKFILE without first backing up the log is ineffective because active VLFs cannot be shrunk. Always back up first, then shrink. Also, avoid shrinking logs repeatedly โ frequent shrink/grow cycles cause VLF fragmentation, which degrades write performance over time.
Emergency Option: Switch to Simple Recovery Model
If you cannot take a log backup immediately (e.g., no prior full backup exists), you can switch the database to SIMPLE recovery model as an emergency measure. In SIMPLE mode, SQL Server automatically truncates the log at every checkpoint, eliminating the need for log backups.
๐ต FULL Recovery
All operations fully logged. Point-in-time restore possible. Requires regular log backups or log grows forever.
๐ข BULK_LOGGED
Bulk operations minimally logged. Point-in-time restore mostly supported. Still requires log backups.
๐ก SIMPLE Recovery
Auto-truncates log at checkpoint. No log backups needed. No point-in-time restore. Only restore to last full/differential backup.
-- EMERGENCY ONLY: Switch to SIMPLE recovery model -- WARNING: You lose point-in-time restore capability after this change ALTER DATABASE [YourDatabaseName] SET RECOVERY SIMPLE; GO -- Force a checkpoint to truncate the log immediately USE [YourDatabaseName]; CHECKPOINT; GO -- Now shrink the freed space DBCC SHRINKFILE (N'YourDatabase_log', 256); GO -- IMPORTANT: Switch back to FULL when done and take a full backup immediately ALTER DATABASE [YourDatabaseName] SET RECOVERY FULL; GO BACKUP DATABASE [YourDatabaseName] TO DISK = N'D:\Backups\YourDatabase_full_afterchange.bak';
Once you switch to SIMPLE mode, your existing log backup chain is broken. Any existing log backups taken before the switch can no longer be applied after a restore. After switching back to FULL, you must immediately take a new full database backup to restart a valid log backup chain. Never switch production OLTP databases to SIMPLE permanently without understanding the recovery implications.
Prevent It: Schedule Regular Log Backups with SQL Agent
The single most effective way to prevent Error 9002 is to schedule automated transaction log backups using SQL Server Agent. For a busy OLTP database, log backups every 15โ30 minutes are standard. Here is a complete SQL Agent job script you can run immediately in SSMS:
USE msdb; GO EXEC sp_add_job @job_name = N'Transaction Log Backup - YourDatabase', @enabled = 1, @description = N'Backs up transaction log every 15 minutes to prevent log full errors'; EXEC sp_add_jobstep @job_name = N'Transaction Log Backup - YourDatabase', @step_name = N'Backup Log', @command = N'BACKUP LOG [YourDatabaseName] TO DISK = N''D:\Backups\YourDatabase_log_'' + REPLACE(CONVERT(varchar,GETDATE(),112),'''','''') + ''_'' + REPLACE(CONVERT(varchar,GETDATE(),108),'':'','''') + ''.bak'' WITH COMPRESSION, STATS = 10;'; EXEC sp_add_schedule @schedule_name = N'Every 15 Minutes', @freq_type = 4, -- Daily @freq_interval = 1, @freq_subday_type = 4, -- Minutes @freq_subday_interval = 15; EXEC sp_attach_schedule @job_name = N'Transaction Log Backup - YourDatabase', @schedule_name = N'Every 15 Minutes'; EXEC sp_add_jobserver @job_name = N'Transaction Log Backup - YourDatabase'; GO
For a comprehensive, production-grade backup strategy, consider deploying Ola Hallengren's SQL Server Maintenance Solution โ a free, widely trusted set of stored procedures that handles full, differential, and log backups with intelligent retention, compression, and notification built in.
Understanding SQL Server Error 9002 in Detail
Error 9002 is raised at severity level 17 (resource error), which means SQL Server cannot complete the requested operation due to a resource constraint โ in this case, available log space. Every write statement executed after Error 9002 is raised will immediately fail with the same error until log space is freed.
The parenthetical value in the error message โ e.g., "due to 'LOG_BACKUP'" โ is the log_reuse_wait_desc from sys.databases and directly tells you what is preventing log reuse. Here is the full reference:
| log_reuse_wait_desc Value | Meaning | Fix |
|---|---|---|
| LOG_BACKUP | No log backup has been taken | Run BACKUP LOG |
| ACTIVE_TRANSACTION | Open uncommitted transaction | Identify and COMMIT or ROLLBACK |
| REPLICATION | Log Reader Agent is behind | Fix/restart replication agent |
| DATABASE_MIRRORING | Mirror is not acknowledging log | Fix mirror connectivity |
| CHECKPOINT | Checkpoint hasn't run yet | Run CHECKPOINT; or switch to SIMPLE |
Automate Log Backups & Recovery With Data Repair Pro
Data Repair Pro includes a built-in Transaction Log Manager that shows you the current log usage for every database, identifies the reuse wait reason in plain English, and guides you through taking a log backup or shrinking the log file โ all through a clean graphical interface. No T-SQL required.
- โ Visual log space dashboard
- โ One-click log backup
- โ Automated DBCC SHRINKFILE
- โ Recovery model warnings
- โ SQL Agent job creator
~12 MB ยท Windows 10/11 (64-bit) ยท SQL Server 2012โ2025
Frequently Asked Questions
Can I just delete the .LDF file to fix a full transaction log?
No โ never delete an .LDF file while SQL Server is running. The transaction log is an active, essential part of database integrity. Deleting it while the database is online will immediately crash the database and put it in SUSPECT state. If you need to clear log space, always use BACKUP LOG or switch to SIMPLE recovery model.
How do I find and kill a long-running open transaction?
Query sys.dm_exec_sessions and sys.dm_tran_active_transactions to identify sessions with open transactions older than a few minutes. Once identified, use KILL [session_id] โ but always confirm with the application team first, as killing an active session may trigger a large rollback that itself generates log records.
Should I set max log file size or let it grow automatically?
Setting a maximum size prevents a runaway transaction from consuming all disk space, but it can cause Error 9002 if legitimate workloads exceed the cap. The better approach is to leave autogrowth enabled, monitor log usage proactively, and run frequent log backups so the log never needs to grow beyond a predictable size in the first place.
Will DBCC SHRINKFILE always reclaim space after a log backup?
Not always. If there is still an active transaction, replication lag, or a mirroring delay, some VLFs at the end of the log cannot be freed. Check log_reuse_wait_desc after the backup to confirm that NOTHING is still holding the log. Once it reads NOTHING, DBCC SHRINKFILE will be fully effective.
What is the ideal transaction log file size?
A well-tuned log file should be pre-sized large enough to contain roughly 2โ4 hours of log activity without needing to autogrow. This minimizes VLF fragmentation. For most OLTP databases, setting the initial log size to 1โ4 GB and autogrowth to 256 MBโ1 GB in fixed increments (not percentage) is a solid baseline. Monitor weekly and adjust as the workload grows.
Ready to stop fighting log full errors for good?
Download Data Repair Pro โ Free~12 MB ยท Windows 10/11 ยท No credit card required