Azure Storage Documentation

Comprehensive guides and API references for Azure Storage services.

Clearing Azure Storage Queues

This document explains how to clear all messages from an Azure Storage Queue. Clearing a queue is a common operation when you need to reset the queue's state or prepare it for a new batch of messages.

Important: Clearing a queue permanently deletes all messages currently in it. This operation cannot be undone. Ensure you have backed up any critical data before proceeding.

There are several methods to clear a queue, depending on your preference and the tools you are using:

1. Using the Azure Portal

The Azure Portal provides a user-friendly interface for managing your storage queues.

  1. Navigate to your Storage Account in the Azure Portal.
  2. In the left-hand menu, under "Data storage", select "Queues".
  3. Click on the name of the queue you want to clear.
  4. On the queue's overview page, you will see an option to "Clear all messages". Click this button.
  5. A confirmation dialog will appear. Type the name of the queue to confirm and then click "Clear".

2. Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for automating Azure tasks.

To clear a queue using Azure CLI, you can use the az storage queue clear command.


az storage queue clear --name <queue-name> --account-name <storage-account-name> [--connection-string <storage-connection-string>] [--auth-mode login]
            

Example:


az storage queue clear --name my-message-queue --account-name mystorageaccount123
            

3. Using Azure PowerShell

Azure PowerShell provides cmdlets for managing Azure resources.

Use the Clear-AzStorageQueue cmdlet to clear a queue.


Clear-AzStorageQueue -Name <queue-name> -Context <storage-account-context>
            

Example:


$ctx = New-AzStorageContext -StorageAccountName "mystorageaccount123" -StorageAccountKey "YOUR_STORAGE_ACCOUNT_KEY"
Clear-AzStorageQueue -Name my-message-queue -Context $ctx
            

Alternatively, if you have logged in with Connect-AzAccount:


Clear-AzStorageQueue -Name my-message-queue -StorageAccountName "mystorageaccount123"
            

4. Using SDKs (e.g., .NET, Python, Java, Node.js)

You can also programmatically clear a queue using the Azure Storage SDKs.

.NET Example

Using the Azure.Storage.Queues NuGet package:


using Azure.Storage.Queues;

// Replace with your actual connection string and queue name
string connectionString = "YOUR_CONNECTION_STRING";
string queueName = "my-message-queue";

// Get the queue client
QueueClient queueClient = new QueueClient(connectionString, queueName);

// Ensure the queue exists (optional, but good practice)
queueClient.CreateIfNotExists();

// Clear all messages from the queue
queueClient.ClearMessages();

Console.WriteLine($"Queue '{queueName}' has been cleared.");
            

Python Example

Using the azure-storage-queue library:


from azure.storage.queue import QueueClient

# Replace with your actual connection string and queue name
connection_string = "YOUR_CONNECTION_STRING"
queue_name = "my-message-queue"

# Get the queue client
queue_client = QueueClient.from_connection_string(connection_string, queue_name)

# Ensure the queue exists (optional)
queue_client.create_queue()

# Clear all messages from the queue
queue_client.clear_messages()

print(f"Queue '{queue_name}' has been cleared.")