Delete messages in service broker queue

senna picture senna · May 31, 2012 · Viewed 57.3k times · Source

I'd like to clear my queue in SQL Server Management Studio, but I don't want to delete the whole queue just the content in the queue (the messages).

Answer

Rebecca Campbell picture Rebecca Campbell · Oct 10, 2013

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