Purge Queue
Clear all pending messages from a KubeMQ queue channel using the C# SDK management API.
Overview
Purging a queue is a management-plane operation for wiping a channel's backlog without receiving and discarding messages one at a time. Reach for it when a bad producer floods a channel, when you need a clean slate between test runs, or when you're resetting a queue during a maintenance window — all without deleting and recreating the channel itself.
PurgeQueueAsync(channelName) tells the broker directly to acknowledge and drop every message still pending on the channel, entirely server-side. The returned result's AffectedMessages field reports exactly how many messages were removed, so a 0 tells you the queue was already empty.
Gotchas: the purge is irreversible — there's no undo once messages are removed. It only reaches messages still waiting in the queue; anything already delivered to and held by an active consumer is untouched, so a purge run right after a receive can still leave stragglers. And purging empties the channel, it doesn't delete it — new messages can be enqueued immediately afterward.
Prerequisites
- KubeMQ server running on
localhost:50000 - C# SDK installed (
dotnet add package KubeMQ.SDK.CSharp)
Code
// KubeMQ .NET SDK — Management: Purge Queue
//
// This example demonstrates purging all messages from a queue channel
// using the PurgeQueueAsync API.
//
// Prerequisites:
// - KubeMQ server running on localhost:50000
// - dotnet run
using KubeMQ.Sdk.Client;
await using var client = new KubeMQClient(new KubeMQClientOptions
{
ClientId = "csharp-config-purge-queue-client",
});
await client.ConnectAsync();
Console.WriteLine("Connected to KubeMQ server");
var result = await client.PurgeQueueAsync("csharp-config.purge-queue");
Console.WriteLine($"Purged {result.AffectedMessages} messages");
Console.WriteLine("Done.");
How It Works
PurgeQueueAsync(channelName)sends a server-side purge request; the broker atomically removes all pending (unacknowledged) messages from the queue channel.- The returned result contains
AffectedMessages— the count of messages that were deleted; a value of0means the queue was already empty. - Purging is irreversible: deleted messages cannot be recovered, so use with care in production.
- The channel itself is not deleted by a purge; new messages can be enqueued immediately after.
Related
Was this page helpful?