Repairing an LDF file means restoring usable transaction-log access for a SQL Server database while keeping the data files consistent, so the safest first question is whether you need the log's contents or only a working database. Most "repairs" are really log rebuilds: you detach the database, move the damaged LDF aside, and let SQL Server create a fresh log, which commits anything already written to the MDF and loses uncommitted transactions. If you actually need transactions from a damaged log, for example to recover a recent change, the path is a specialist log-recovery tool, not a routine command. Everything below assumes SQL Server on Windows with a current full backup and enough disk space to hold copies of both files.

First Separate Log Damage From a Bad Connection

Symptom and cause get mixed together constantly. "Cannot open database" errors, a database stuck in Recovery Pending, or a suspect database after a crash can all look like LDF damage, but the log may be fine while the MDF needs attention, or the service account may have lost rights to the file.

Work through a short diagnostic pass before touching any file:

1: Confirm the error in the SQL Server error log and the Windows Application log. Note the numeric message, the database name, and the file path. 2: Check that the SQL Server service account still has Full Control on the data folder. A restored volume or changed service account frequently breaks file access without any corruption. 3: Run DBCC CHECKDB on any database you can open, and DBCC CHECKFILEGROUP where relevant, so you know whether the MDF is healthy. A log rebuild is pointless if the data file is also damaged. 4: Verify free disk space on the log volume. A full disk produces log-write errors that imitate corruption. 5: Copy the MDF and LDF to a safe location before any repair attempt. These copies are your rollback.

Stopping condition: if DBCC CHECKDB reports allocation or consistency errors in the data file, or if the database is a production system with a recovery-point obligation, pause and involve your database administrator or a data-recovery specialist. Rebuilding the log on top of a damaged MDF can make later recovery harder.

Understand What the LDF Holds Before You Delete It

The transaction log records every change in order, plus the information needed to roll back uncommitted work and to redo committed work after a crash. During normal operation SQL Server manages its own recovery from that log; you rarely "repair" it in place.

That creates the central trade-off. If you replace a damaged log, SQL Server can generally attach the database and roll forward committed transactions that reached the data file, but transactions that existed only in the lost log cannot be replayed. A nightly full backup does not preserve those individual transactions; only log backups or a log-recovery tool do. So decide early: rebuilding is fast and routine, while extracting transactions from a damaged log is slow, uncertain, and usually outsourced.

Also, the rebuild only works cleanly when the database was shut down in a consistent state or when you accept that the log's contents are gone. Microsoft's documented procedure for a missing or damaged log uses ATTACH_FORCE_REBUILD_LOG, which forces a rebuild and accepts the resulting loss. That is the supported route when you have no log backup and no need for point-in-time recovery.

Take the Safe Prerequisites Before Any Repair

Before starting, gather four things:

A verified full backup of the database, plus any log backups, stored somewhere other than the failing server. A copy of the MDF and LDF files in a separate folder. Enough free space on the log drive for a new, correctly sized log. A maintenance window, because you will detach the database or take it offline.

Confirm the SQL Server version too. The rebuild syntax and the behavior of file activation differ between major versions, and the procedure below assumes SQL Server 2012 or later with the standard ALTER DATABASE ... MODIFY FILE and CREATE DATABASE ... FOR ATTACH_REBUILD_LOG statements. On older versions, the supported route is to attach without the log through Enterprise Manager or equivalent scripts, so check your version's documentation first.

Scenario One: The Log Is Missing or Won't Open and You Only Need the Database

This is the common case: the LDF is gone, zeroed, or unrecoverable, and there is no log backup to replay.

1: Take the database offline or detach it, and confirm the service account can read the MDF. 2: Move the damaged LDF to a quarantine folder. Do not delete it yet; a recovery tool may need it. 3: Rebuild the log with the attach command, adjusting paths to your instance:

CREATE DATABASE YourDb ON (FILENAME = 'D:\Data\YourDb.mdf') FOR ATTACH_REBUILD_LOG;

4: If the MDF requires additional files (secondary data files), list them in the ON clause before the rebuild option. 5: Watch the Messages tab. A successful attach reports that the log was rebuilt and the database came online. 6: Run DBCC CHECKDB immediately, then take a full backup. 7: Reset the recovery model if you changed it. A rebuilt log restarts the log chain, so any log backups made before the rebuild cannot be applied after it.

Check for success two ways: the database appears online in sys.databases with a valid log_reuse_wait_desc, and a test query against a known table returns expected rows. If the attach fails with a file-access error, fix permissions rather than retrying the rebuild; repeated attach attempts against the same error won't help.

Two limitations matter here. First, if the database was participating in log shipping or an availability group, that participation is broken and must be re-established. Second, a rebuilt log means point-in-time recovery for the gap is gone. For a reporting copy that's fine; for an order system it may not be.

Scenario Two: The Log Is Present but Reported as Corrupt

When SQL Server flags log corruption, the error log usually names the database and a specific corruption message. Recovery depends on whether the database can be brought online at all.

If the database opens and DBCC CHECKDB passes on the data but log reads fail, the practical options are a log backup followed by a rebuild, or a rebuild directly if no current log backup exists. If the database stays in Recovery Pending, first try to bring it online with a restart and a check of the error log; a transient I/O failure sometimes resolves after the storage layer is repaired. Do not run repeated restarts as a strategy; each one writes to the log.

For a log that is corrupt but holds transactions you need, the supported tools are limited. SQL Server's own repair options in DBCC CHECKDB target data-file consistency, and log-level repair is not among the standard options. A third-party log reader can sometimes extract committed transactions from a damaged log into a script or table, but results depend on how much of the log structure survives. Treat those tools as a second opinion, and never run one against the only copy of the files.

Stopping condition: if the database holds regulated or critical records, or if a legal retention or audit obligation applies, get specialist help before rebuilding. The rebuild itself is not reversible once new transactions are written.

Scenario Three: The Log Is Enormous and Something Is Wrong

A log that has grown to fill the disk is usually not corrupt; it is waiting on something. The fix is diagnosis, not repair. Check log_reuse_wait_desc in sys.databases for the database. Common results point to a full backup, a log backup, or an availability-group replica that isn't keeping up.

If the database is in Full recovery and no log backups are being taken, the log will grow indefinitely. Taking a log backup truncates the inactive portion; switching to Simple recovery does the same immediately but breaks the log chain, so only do that when point-in-time recovery is not needed. A log backup taken after a switch to Simple cannot restore forward, so confirm the recovery requirement before changing the model back.

If log_reuse_wait_desc shows ACTIVE_TRANSACTION, a long-running transaction is holding the log open. Find it with sys.dm_tran_database_transactions joined to sys.dm_exec_sessions, then decide whether to let it finish or terminate it. LOG_BACKUP or FULL means the log is waiting for its backup. A shrink will only help after the wait reason clears, and shrinking a log below its needed size just causes regrowth.

Edge case worth knowing: a log file that was manually increased in an unsupported way, such as a file with a nonzero starting offset or a file extension copied from another database, can appear as a size mismatch or fail to attach. The standard response is still the rebuild path, with the original file preserved for inspection.

Decide Between Rebuild, Restore, and Recovery Tool

Three routes exist, and they differ by what you get back.

A log rebuild gives you a working database quickly and loses anything only in the lost log. Restoring from a full backup plus log backups gives you a point-in-time database up to the last good backup, but nothing after it, and it requires that those backups exist and verify. A specialist log-recovery tool gives you a chance at transactions after the last backup, at the cost of time, money, and uncertain results.

The comparison below assumes the same database, the same server, and a full backup taken the same night. Units are informal because the real numbers depend on database size and backup cadence.

Criterion Rebuild log Restore from backups Recovery tool
Typical time Minutes to an hour Depends on backup size and network Hours to days
Data after last backup Lost Lost Possibly recoverable
Requires valid backups No Yes No
Risk to MDF Low if file is healthy None Depends on tool handling
Repeatable Yes Yes No, results vary

The practical rule: if the database can tolerate losing the transactions since the last backup, rebuild or restore; pick restore when backups are current, rebuild when they are not or when restore would take longer than the outage allows.

One nuance in the table: restore is not strictly "no data after last backup" if log backups exist, because applying them moves the recovery point forward. The row reflects the common case where the log backups are the very thing that is missing.

What "Repair" Can't Do

The rebuild path cannot reconstruct individual transactions. It cannot restore point-in-time recovery across the gap. It cannot preserve availability-group membership, log-shipping configuration, or replication state; those need reconfiguration after the database is online. It also cannot fix a damaged MDF, and repeated rebuilds against a failing disk will keep failing until the storage problem is addressed.

An honest limit: if the only copy of the log is on a failing drive, stop writing to that drive immediately and copy the files elsewhere first. Each additional read or write reduces the chance a recovery tool can extract anything.

For routine prevention, three habits cover most incidents: keep log backups on a schedule matched to your recovery tolerance, monitor log_reuse_wait_desc rather than reacting to disk alarms, and store the LDF on a volume separate from the MDF when the workload justifies it.

FAQ

Can I repair an LDF with a built-in SQL Server command? Not in the sense of fixing a damaged log's contents. The supported route is a rebuild through FOR ATTACH_REBUILD_LOG, which replaces the log rather than repairing it, plus restore from valid backups.

Does a rebuilt log affect my ability to restore later? Yes. The log chain restarts, so log backups taken before the rebuild cannot be applied afterward. Take a full backup immediately after the rebuild to start a new chain.

Will a rebuild work with an MDF that is also damaged? Not reliably. Run DBCC CHECKDB first, and if the data file shows consistency errors, use a backup or specialist recovery instead of forcing a rebuild.

Is a huge log a sign of corruption? Usually not. It typically means log backup isn't running, a long transaction is open, or an availability-group replica is behind. Check log_reuse_wait_desc before assuming damage.

Should I delete the damaged LDF after the rebuild? Keep it in quarantine until the database has been verified with DBCC CHECKDB and a post-rebuild backup exists. It is the only artifact a recovery tool could use if the rebuild turns out to be the wrong call.