Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a quick way of dropping a Service Broker queue

I have been running a load test that involves a Service Broker queue over night. Something went wrong and the queue ended up with 1.3 million messages in it. I just want to clear these out, no backup and no logging required. The trouble is that when I try to drop the queue it is taking half an hour and then the log file fills up and the whole thing rolls back. Is there a quick and dirty way of flushing everything out the queue?

like image 349
Martin Brown Avatar asked Sep 17 '25 10:09

Martin Brown


2 Answers

You can clear all conversations at once with the following statement:

declare @conversationHandle uniqueidentifier 
select top 1 @conversationHandle = conversation_handle from sys.conversation_endpoints    
while @@rowcount = 1

begin    
     end conversation @conversationHandle with cleanup    
     select top 1 @conversationHandle = conversation_handle from sys.conversation_endpoints    
end
like image 145
GaussZ Avatar answered Sep 21 '25 09:09

GaussZ


If you omit the TOP 1 clause in the RECEIVE statement then it will receive all messages that meet the criteria, or setting it to a high number will allow you to clear the backlog in managable chunks.

The trick will be to 'blackhole' the messages so the result set isn't returned to the client.

like image 28
Stephen Turner Avatar answered Sep 21 '25 07:09

Stephen Turner