Shrinking a Log Without Breaking Log Shipping

Do not switch the primary database to SIMPLE recovery to empty the log. The primary must use the full or bulk-logged recovery model, and switching to simple recovery stops log shipping. Find what blocks truncation first. Let the log backup job run. Then shrink the file, which only helps if the log has unused space.
Symptom
The log file on the primary is large. Setting SIMPLE recovery to empty it is tempting, but on a log-shipped database that switch stops log shipping.
Cause
Log shipping is built on log backups. It consists of three operations, and the first is the transaction log backup on the primary server instance. The docs list full or bulk-logged as the required recovery model for the primary.

A shrink is also limited. A shrink only makes a difference when a transaction log file contains unused space. No part of the active log can ever be truncated. If the log stays full, open transactions are the usual reason, and the docs say to investigate what is preventing truncation before shrinking.
Fix
The steps below are working practice, not quotations from the docs, except where a link is given.

Take a full backup first. Then find the truncation blocker.
-- Recovery model must stay FULL or BULK_LOGGED
SELECT name, recovery_model_desc, log_reuse_wait_desc
FROM sys.databases
WHERE name = N'YourDb';
-- Oldest active transaction in the database
DBCC OPENTRAN (N'YourDb');- Read
log_reuse_wait_desc. A value ofACTIVE_TRANSACTIONpoints to an open transaction;DBCC OPENTRANshows the oldest one. A value ofLOG_BACKUPmeans the log is waiting for a log backup, not for a transaction. - For an open transaction, commit it, roll it back or let it finish. Check what it is before killing it.
- For
LOG_BACKUP, let the log shipping backup job run on the primary so the log backup happens as normal. - Shrink only after the log has unused space.
USE YourDb;
-- Target size in MB; pick a size that fits normal activity
DBCC SHRINKFILE (N'YourDb_log', 1024);A shrink returns free space to the operating system by reducing the physical size of the log.
Check the result
SELECT name, recovery_model_desc, log_reuse_wait_desc
FROM sys.databases
WHERE name = N'YourDb';
SELECT name, size * 8 / 1024 AS size_mb
FROM sys.database_files
WHERE type_desc = N'LOG';recovery_model_descstill readsFULLorBULK_LOGGED.size_mbis lower than before.- The log shipping backup, copy and restore jobs still succeed.
When it does not apply
- The shrink changes nothing if the log has no unused space. The active part stays, so the blocker must be cleared first.
- A log that grows again right after the shrink needs a sizing decision, not another shrink.
- The database is not log-shipped and SIMPLE recovery is already the intended model. Then this note does not apply.
Docs: About log shipping · Configure log shipping · Manage the size of the transaction log file · Transaction log architecture

More notes