Azure Documentation

How to Clear an Azure Storage Queue

This tutorial demonstrates how to programmatically clear all messages from an Azure Storage Queue. This operation is useful for resetting a queue to an empty state, typically for testing or during maintenance.

Important: Clearing a queue permanently deletes all messages. Ensure you have a backup or no longer need the existing messages before proceeding.

Prerequisites

Understanding the Clear Queue Operation

The Azure Storage SDKs provide a straightforward method to clear a queue. This operation removes all messages from the queue, effectively making it empty. It's an atomic operation, meaning it either succeeds completely or fails entirely.

Using Azure SDKs

1. .NET Example

This example uses the Azure.Storage.Queues NuGet package.

C#
using Azure.Storage.Queues;
using System;
using System.Threading.Tasks;

public class QueueClearer
{
    public static async Task ClearQueueAsync(string connectionString, string queueName)
    {
        // Instantiate a QueueClient
        QueueClient queueClient = new QueueClient(connectionString, queueName);

        try
        {
            // Ensure the queue exists (optional, but good practice)
            await queueClient.CreateIfNotExistsAsync();
            Console.WriteLine($"Accessing queue: {queueName}");

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

            Console.WriteLine($"Successfully cleared all messages from queue: {queueName}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error clearing queue: {ex.Message}");
        }
    }

    // Example usage:
    public static async Task Main(string[] args)
    {
        // Replace with your actual connection string and queue name
        string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
        string queueName = "my-sample-queue";

        if (connectionString == "YOUR_AZURE_STORAGE_CONNECTION_STRING")
        {
            Console.WriteLine("Please replace 'YOUR_AZURE_STORAGE_CONNECTION_STRING' with your actual connection string.");
            return;
        }

        await ClearQueueAsync(connectionString, queueName);
    }
}

2. Node.js Example

This example uses the @azure/storage-queue npm package.

JavaScript (Node.js)
const { QueueClient } = require("@azure/storage-queue");

async function clearQueue(connectionString, queueName) {
    const queueClient = new QueueClient(connectionString, queueName);

    try {
        // Ensure the queue exists (optional, but good practice)
        await queueClient.createIfNotExists();
        console.log(`Accessing queue: ${queueName}`);

        // Clear all messages from the queue
        const result = await queueClient.clearMessages();

        console.log(`Successfully cleared queue: ${queueName}`);
        console.log(`Messages cleared: ${result.size}`);
    } catch (error) {
        console.error(`Error clearing queue: ${error.message}`);
    }
}

// Example usage:
async function main() {
    // Replace with your actual connection string and queue name
    const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    const queueName = "my-sample-queue";

    if (connectionString === "YOUR_AZURE_STORAGE_CONNECTION_STRING") {
        console.log("Please replace 'YOUR_AZURE_STORAGE_CONNECTION_STRING' with your actual connection string.");
        return;
    }

    await clearQueue(connectionString, queueName);
}

main().catch(console.error);

Important Considerations

Create Azure Storage Account Get Started with Azure Queues