Delete messages in service broker queue
I would use end conversation (that will also remove all related messages from all queues) using statement:
End Conversation @c With CleanUp
if you just receive message, then you leave conversation open. End Conversation With CleanUp is for specific situations only.
Something like this should work:
while(1=1)
begin
waitfor (
receive top(1)
conversation_group_id
from dbo.yourQueue
), timeout 1000;
if (@@rowcount = 0)
break;
end
Just combining the two previous answers (by Ben and Jānis) for clarity. This worked for me:
declare @c uniqueidentifier
while(1=1)
begin
select top 1 @c = conversation_handle from dbo.queuename
if (@@ROWCOUNT = 0)
break
end conversation @c with cleanup
end