Need to delete Millions of records from table which is part of Transaction Replication

Sowjanya Gopal 120 Reputation points
2026-09-21T19:27:54.4666667+00:00

I have transaction replication on a table. I have to delete millions of records from it. I want to create a SP to delete rows & replicate execution of SP. A few records mismatch might be there between Publisher & Subscriber. Will it cause issue.

And I am confused if I have to Replicate Execution of Stored Proc / Execution in a Serialized Transaction of the SP.

 

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author
Deepesh Dhake 1,245 Reputation points
2026-09-22T18:03:19.8466667+00:00

Use plain proc exec (not serializable). Serializable silently reverts to millions of row-by-row DML commands if any call runs outside a serializable transaction - the exact volume you're avoiding. You've waived consistency, so plain proc exec is simpler with no such failure mode.

Why it won't break replication: your EXEC bypasses the default per-row delete procs that raise "row not found" (20598). A Publisher-only in-range row just matches nothing on the Subscriber no error, no stall.

Pattern bounded ranges, looped EXEC (the loop isn't replicated, each EXEC is):

CREATE PROCEDURE dbo.usp_DeleteByRange @RangeStart BIGINT, @RangeEnd BIGINT
AS
BEGIN
    SET NOCOUNT ON;
    DELETE FROM dbo.YourTable
    WHERE YourKeyColumn BETWEEN @RangeStart AND @RangeEnd;  -- DateKey or PK
END;

DECLARE @lo BIGINT = 1, @hi BIGINT = 50000000, @batch BIGINT = 20000;
WHILE @lo <= @hi
BEGIN
    EXEC dbo.usp_DeleteByRange @lo, @lo + @batch - 1;
    WAITFOR DELAY '00:00:01';
    SET @lo += @batch;
END;

Rules:

Pass bounds as literal parameters so no GETDATE() inside the proc (it re-evaluates on the Subscriber).

Index the predicate on both sides - the Subscriber re-runs the same delete; an unindexed scan there is your main latency risk.

Keep the proc minimal and batches small (lock/log control).

DateKey (YYYYMMDD) isn't contiguous - step by PK range, or by actual distinct DateKeys.

Was this answer helpful?

2 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. PiusR 0 Reputation points
    2026-09-22T08:14:01.1866667+00:00

    Could you imagine to go with normal DELETE in batches?

    For example delete 50k rows, commit, then continue with next batch. This should be much better for transaction log and replication than one very big delete.

    I would be careful with stored procedure replication here. If publisher and subscriber already aren't 100% the same, same DELETE condition can remove different rows on subscriber.

    So better first try batched delete on publisher and let replication handle the row deletes. check the replication latency and log growth.

    If this is still too slow (because of very huge number of rows), then stored procedure execution can be considered, but only if both sides are really in sync.

    Was this answer helpful?

    0 comments No comments

  2. Harsh meena 0 Reputation points
    2026-09-22T07:22:33.0666667+00:00

    If you execute a normal DELETE statement on the Publisher, transactional replication reads the logged row changes from the transaction log and sends those DELETE operations to the Subscriber. In that case, existing data mismatches are usually less of a concern because the replicated commands target specific rows identified by the article's key columns.

    However, if you configure stored procedure execution replication, the procedure itself is executed on the Subscriber. If the Publisher and Subscriber are not perfectly synchronized, a criteria-based delete such as:

    Was this answer helpful?

    0 comments No comments

  3. Bruce (SqlWork.com) 85,456 Reputation points
    2026-09-21T19:59:35.27+00:00

    transaction replication uses the transaction log (recorded page changes). all logged transaction appear in the log. as long as the sp does not perform unlogged transactions (bulk insert or truncate table), then it will replicate properly.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.