1
votes

I would like to delete the Dead Lettered messages from the service bus queue. In particular, this value is called DeadLetterMessageCount and you can find out this by right-clicking the "Properties" of the SB queue in the Server Explorer of your project (in case of using a SB queue).

The reason I would like to do this is because I've set up an Autoscale of the cloud service. So, when the SB queue is quite big, it adds some more cores in order to proceed the messages faster (it enables more worker roles). I realized that when you set up the scaling depending on the number of messages in the queue, it counts the DeadLettered messages as well (messages that cannot be consumed). So that's a waste of money, as more instances are enabled that are not needed.

Any queries, please let me know.

Thanks for your help

3
"I realized that when you set up the scaling depending on the number of messages in the queue, it counts the DeadLettered messages as well" - do you have any links to where you found this information?Haohmaru
@Haohmaru I'm not sure about this but I will explain you my conclusion. In the Microsoft Azure Dashboard, in the right column there is a field called "Queue Length". This shows the number of messages in the queue, active and dead lettered. This is the number that the scaling takes into account in order to change the number of instances that are needed. If you have a different opinion, please let me knowKatsifaris

3 Answers

4
votes

You read and delete messages from dead letter queue the same way you read from normal queues or subscriptions.

You can use this method to get the path of the queue: QueueClient.FormatDeadLetterPath(queuePath).

Also see this previous answer: How do I delete a DeadLetter message on an Azure Service Bus Topic

0
votes

This is a code to delete a Dead-Letter messages from Queues.

public async void DeleteMessagesFromQueueAsync()
    {
        bool isDeadLetter=true;
        long SequenceNumber = 12;
        string queuePath='queue name';
        string connectionString='connection string of ASB Namespace';

        BrokeredMessage _srcMessage = null;
        DeleteMessageResponse _msgDeletionStatus = new DeleteMessageResponse();
        MessageReceiver fromQueueClient = null;
        try
        {
            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);

            string _fromEntityPath = !isDeadLetter ? queuePath : QueueClient.FormatDeadLetterPath(queuePath);

            fromQueueClient = await factory.CreateMessageReceiverAsync(_fromEntityPath, ReceiveMode.PeekLock);

                BrokeredMessage _message = await fromQueueClient.ReceiveAsync(SequenceNumber);
                if (_message != null)
                    _srcMessage= _message;

            if (_srcMessage != null )
            {  
                    await _srcMessage.CompleteAsync();
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (fromQueueClient != null)
                await fromQueueClient.CloseAsync();
        }
    }
0
votes

You can use 'ReceiveAndDelete' mode and 'ReceiveBatchAsync' to delete quickly from DeadLetterQueue

private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var DLQPath = "/$DeadLetterQueue"; ///**** Important - Pointing to DLQ'

                var topicName = "message";
                var sub = "message-subscription";
                int batchSize = 100;

                runProcess = true;

                _subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionSt, topicName, sub + DLQPath, ReceiveMode.ReceiveAndDelete);

                int cnt = 0;

                do
                {
                    var messages = await _subscriptionClient.ReceiveBatchAsync(batchSize);
                    var msgCount = messages.Count();

                    if (msgCount == 0)
                    {
                        break;
                    }
                    cnt += msgCount;
                    labelCount.Text = cnt.ToString();
                }
                while (runProcess);

                _subscriptionClient.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().Message);
                return;
            }
        }