Skip to content

Add is_empty() method to QueueClient #44631

@adendek

Description

@adendek

Is your feature request related to a problem? Please describe.

I am frustrated by the lack of a straightforward, reliable way to check if a queue is truly empty. Currently, a common workaround is to attempt to receive a message with a short visibility timeout:

def is_empty(self) -> bool: 
   return self.queue_client.receive_message(visibility_timeout=0) is None

However, this approach is flawed. It yields a false positive if the queue contains messages that are currently invisible (e.g., being processed by another worker). In this scenario, the method returns True, even though the queue is not technically empty and will have messages available again once visibility timeouts expire.

Describe the solution you'd like

I would like a built-in is_empty() method (or a similar property) on the QueueClient class. This method should ideally check the metadata of the queue to determine if there are any messages present, regardless of their visibility state.

Additional context

In a multi-consumer environment, the current "receive" hack causes logic errors where a system might shut down or trigger a "cleanup" task believing the work is done, while other consumers are still holding onto messages.

Below is the current wrapper implementation I am using, which highlights the need for a more robust built-in solution:

class MyQueueClient():
   def __init__(self, queue_name: str = "my-queue-test", connection_option: str = ""):
    self.queue_service_client = (
        self.__get_queue_service_from_connection_string(connection_option)
        if self.__authenticate_fron_connection_str(connection_option)
        else self.__get_queue_service_from_default_credential(connection_option)
    )

    try:
        self.queue_client = self.queue_service_client.create_queue(queue_name)
    except ResourceExistsError:
        self.queue_client = self.queue_service_client.get_queue_client(queue_name)

# This is the problematic implementation I am trying to replace:
def is_empty(self) -> bool: 
   return self.queue_client.receive_message(visibility_timeout=0) is None

Metadata

Metadata

Assignees

No one assigned

    Labels

    customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions