KubeMQ
Client SDKsGoHow-to guidesManagement

Delete Channel

Delete an existing KubeMQ messaging channel programmatically using the Go SDK management API.

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(ctx, name, channelType) sends a management call that removes the channel by name and type; the typed convenience wrapper client.DeleteEventsChannel(ctx, name) does the same thing without requiring the ChannelTypeEvents constant. The channelType 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 channel that doesn't exist returns an error rather than succeeding silently, so idempotent cleanup code needs to catch and ignore the not-found case. There's no "soft delete" or recovery window — once it's gone, any messages still queued are gone with it. And subscribers or producers still connected to a deleted channel won't be notified proactively; they'll only discover it on their next publish or receive call.

Prerequisites

  • KubeMQ server running on localhost:50000
  • Go SDK installed (go get github.com/kubemq-io/kubemq-go/v2)

Code

main.go
// Example: management/delete-channel
//
// Demonstrates deleting a channel. First creates a channel, then deletes it.
//
// Channel: go-management.delete-channel
// Client ID: go-management-delete-channel-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/kubemq-io/kubemq-go/v2"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	client, err := kubemq.NewClient(ctx,
		kubemq.WithAddress("localhost", 50000),
		kubemq.WithClientId("go-management-delete-channel-client"),
	)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	channelName := "go-management.delete-channel.temp"

	// Create a channel to demonstrate deletion.
	err = client.CreateChannel(ctx, channelName, kubemq.ChannelTypeEvents)
	if err != nil {
		log.Printf("CreateChannel: %v", err)
	} else {
		fmt.Printf("Created channel: %s\n", channelName)
	}

	// Delete the channel.
	err = client.DeleteChannel(ctx, channelName, kubemq.ChannelTypeEvents)
	if err != nil {
		log.Printf("DeleteChannel: %v", err)
	} else {
		fmt.Printf("Deleted channel: %s\n", channelName)
	}

	// Typed convenience method — no channel-type constant needed.
	typedName := "go-management.delete-channel.typed"
	_ = client.CreateEventsChannel(ctx, typedName)
	err = client.DeleteEventsChannel(ctx, typedName)
	if err != nil {
		log.Printf("DeleteEventsChannel: %v", err)
	} else {
		fmt.Printf("Deleted events channel (typed): %s\n", typedName)
	}
}

How It Works

  1. client.DeleteChannel(ctx, name, channelType) removes the channel from the broker; any unprocessed messages in the channel are discarded.
  2. The typed convenience method client.DeleteEventsChannel(ctx, name) wraps the generic form without requiring the ChannelTypeEvents constant.
  3. The example first creates the channel to guarantee it exists before demonstrating the delete — useful in tests or CI pipelines that want a clean teardown.
  4. Deleting a channel that does not exist returns an error; use errors.As with *kubemq.KubeMQError to inspect the code and skip ErrCodeNotFound in cleanup flows.

Was this page helpful?

On this page