Delete Channel
Delete an existing KubeMQ channel by name and type using the C++ SDK administration API for cleanup and management.
Overview
Deleting a channel is how you decommission a topic, queue, or RPC endpoint you no longer need — tearing down test fixtures between CI runs, retiring a deprecated integration, or cleaning up the throwaway channels a demo or load test created. It's a permanent, immediate operation: the channel's routing entry is removed from the broker and any messages still sitting in it are discarded, so it's not something you want triggered by a typo in a shared environment.
Under the hood, client->DeleteChannel(name, type) sends a management call that removes the channel by name and type; the typed convenience method client->DeleteEventsChannel(name) does the same thing without requiring the kChannelTypeEvents constant. The type argument matters — channels are namespaced by type, so an events channel and a queues channel can share the same name without colliding, and deleting one never touches the other.
Gotchas: deleting a non-existent channel may or may not return an error depending on server configuration, so don't rely on the result status alone to detect whether the channel was actually there. There's no "soft delete" or recovery window — any messages still queued are discarded the moment deletion completes, so double-check the channel name and type before calling this in a shared environment. And subscribers or producers still connected to a deleted channel aren't notified proactively; they only discover it on their next call.
Prerequisites
- KubeMQ server running on
localhost:50000 - C++ SDK installed (vcpkg or CMake FetchContent)
- C++17 compiler (GCC 9+, Clang 9+, MSVC 2019+)
Code
// Example: management/delete_channel
//
// Demonstrates deleting a channel after creating it, including
// typed convenience methods.
//
// Channel: cpp-management.delete-channel
// Client ID: cpp-management-delete-channel-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).
#include <kubemq/kubemq.h>
#include <iostream>
#include <string>
int main() {
std::cout << "[1] Connecting to localhost:50000" << std::endl;
kubemq::ClientOptions options;
options.set_address("localhost", 50000);
options.set_client_id("cpp-management-delete-channel-client");
auto client_result = kubemq::Client::Create(options);
if (!client_result.ok()) {
std::cerr << "[ERROR] Failed to create client: " << client_result.status().message()
<< std::endl;
return 1;
}
auto& client = *client_result;
std::string channel_name = "cpp-management.delete-channel.temp";
// Create a channel to demonstrate deletion.
auto status = client->CreateChannel(channel_name, kubemq::kChannelTypeEvents);
if (!status.ok()) {
std::cerr << "[ERROR] CreateChannel: " << status.message() << std::endl;
} else {
std::cout << "[2] Created channel: " << channel_name << std::endl;
}
// Delete the channel.
status = client->DeleteChannel(channel_name, kubemq::kChannelTypeEvents);
if (!status.ok()) {
std::cerr << "[ERROR] DeleteChannel: " << status.message() << std::endl;
} else {
std::cout << "[3] Deleted channel: " << channel_name << std::endl;
}
// Typed convenience method.
std::string typed_name = "cpp-management.delete-channel.typed";
client->CreateEventsChannel(typed_name);
status = client->DeleteEventsChannel(typed_name);
if (!status.ok()) {
std::cerr << "[ERROR] DeleteEventsChannel: " << status.message() << std::endl;
} else {
std::cout << "[4] Deleted events channel (typed): " << typed_name << std::endl;
}
// Close the client explicitly.
// Note: Client destructor would also handle cleanup (RAII)
auto close_status = client->Close();
if (!close_status.ok()) {
std::cerr << "[ERROR] Close failed: " << close_status.message() << std::endl;
return 1;
}
std::cout << "[5] Client closed" << std::endl;
return 0;
}How It Works
- Creates a channel, then deletes it with
DeleteChannel(name, type). - Also demonstrates the typed convenience method
DeleteEventsChannel(). - Deleting a non-existent channel may return an error depending on server configuration.
- Use with caution in production -- deleting a channel removes all associated data.
Related
Was this page helpful?