Last active
July 27, 2023 19:38
-
-
Save ramonsmits/37606d0f04aac53f5a12a79bb04d6b22 to your computer and use it in GitHub Desktop.
NServiceBus SQL Persistent outbox cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @DispatchedBefore datetime = GETUTCDATE()-1 -- Removes entries older then 24 hours | |
DECLARE @BatchSize INT = 4000 -- Avoid batch sizes over 4.000 to prevent lock escalation | |
WHILE 1 = 1 | |
BEGIN | |
DELETE TOP (@BatchSize) FROM [dbo].[EndpointNameOutboxData] WITH (ROWLOCK) | |
WHERE Dispatched = 'true' AND DispatchedAt < @DispatchedBefore; | |
IF @@ROWCOUNT < @BatchSize -- Important that @@ROWCOUNT is read immediately after the DELETE | |
BREAK | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment