Purge Queue
Purge all messages from a KubeMQ queue channel to clear it using the Rust 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.
ack_all_queue_messages tells the broker directly to acknowledge and drop every message still pending on the channel, entirely server-side. You give it a channel and a wait_time_seconds drain window so the broker can settle in-flight deliveries before finalizing, and it hands back an affected_messages count so you can confirm exactly how much was cleared.
Gotchas: the purge is irreversible — there's no undo once messages are acknowledged away. 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 sent immediately afterward.
Prerequisites
- KubeMQ server running on
localhost:50000 - Rust SDK installed (
cargo add kubemq)
Code
use kubemq::prelude::*;
use kubemq::{AckAllQueueMessagesRequest, QueueMessageBuilder};
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let channel = "rust-management.purge-queue";
for i in 0..10 {
let msg = QueueMessageBuilder::new()
.channel(channel)
.body(format!("purge-msg-{}", i).into_bytes())
.build();
client.send_queue_message(msg).await?;
}
println!("Sent 10 messages");
let req = AckAllQueueMessagesRequest {
request_id: String::new(),
client_id: String::new(),
channel: channel.to_string(),
wait_time_seconds: 5,
};
let resp = client.ack_all_queue_messages(&req).await?;
println!("Purged {} messages from queue", resp.affected_messages);
client.close().await?;
Ok(())
}How It Works
ack_all_queue_messagesacknowledges (and removes) all pending messages from the queue.- This is equivalent to purging the queue — all messages are consumed without processing.
- Review timeouts, channel names, and client IDs before running against shared environments.
- Run the program while the server from the prerequisites is available.
Related
Was this page helpful?