Posted in:

The Azure C# SDK contains the NamespaceManager.GetQueues method to receive a list of all the queues in your Azure Service Bus namespace. This is great if you’ve just got a few queues, but if you have hundreds or even thousands, you might want to filter them.

The good news is that there’s an overload of GetQueues that takes a filter string. The bad news is that the MSDN documentation gives you no clues whatsoever as to what you’re supposed to put in that string.

So after a lot of guesswork, googling and experimentation, here’s what I’ve worked out.

Filter by Queue Name

To get all the queues that start with a particular prefix, you can use the startswith method, and check that it returns true:

var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var myQueues = namespaceManager.GetQueues("startswith(path, 'mheath') eq true");

I did experiment to see if there also were methods available like endswith, contains or substringof, but couldn’t get anything to work.

Filter by Message Count

You can also query for all queues based on their message count (which includes the number of dead-lettered messages). Here we’re looking for queues with over 1000 messages. The queue objects themselves are instances of QueueDescription which has a MessageCountDetails property which allows you to see the breakdown of messages by type (active, dead-letter, scheduled etc).

var backedUpQueues = namespaceManager.GetQueues("messageCount Gt 1000");

Filter by Date

Finally you can query for all queues by their created date, or last accessed or updated date. Here’s a query for inactive queues that haven’t been accessed in the past few months:

namespaceManager.GetQueues("AccessedAt Lt '2016-06-01T00:00:00Z'")

What about Topics and Subscriptions?

NamespaceManager has GetTopics and GetSubscriptions method which also take filter strings, so you should be able to apply the same operators to get filtered topics and subscriptions.