Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Last active July 27, 2023 19:38
Show Gist options
  • Save ramonsmits/37606d0f04aac53f5a12a79bb04d6b22 to your computer and use it in GitHub Desktop.
Save ramonsmits/37606d0f04aac53f5a12a79bb04d6b22 to your computer and use it in GitHub Desktop.
NServiceBus SQL Persistent outbox cleanup
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