Delete channel (v1)

Delete an Eventarc channel.

Code sample

Go

To authenticate to Eventarc, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import (
	"context"
	"fmt"
	"io"

	eventarc "cloud.google.com/go/eventarc/apiv1"
	"cloud.google.com/go/eventarc/apiv1/eventarcpb"
)

// deleteChannel deletes an existing Eventarc channel.
func deleteChannel(ctx context.Context, w io.Writer, projectID string, locationID string, channelID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/channels/%s", projectID, locationID, channelID)

	client, err := eventarc.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient failed: %w", err)
	}
	defer client.Close()

	req := &eventarcpb.DeleteChannelRequest{
		Name: name,
	}

	op, err := client.DeleteChannel(ctx, req)
	if err != nil {
		return fmt.Errorf("DeleteChannel failed: %w", err)
	}

	// DeleteChannel is a long-running operation; wait for it to complete.
	ch, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait failed: %w", err)
	}

	fmt.Fprintf(w, "Deleted channel: %s\n", ch.GetName())
	return nil
}

Node.js

To authenticate to Eventarc, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {EventarcClient} = require('@google-cloud/eventarc');
const {status} = require('@grpc/grpc-js');

const client = new EventarcClient();

/**
 * Deletes an Eventarc channel.
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud region where the channel is located (for example, "us-central1").
 * @param {string} channelId - The ID of the channel to delete.
 */
async function deleteChannel(projectId, locationId, channelId) {
  try {
    const name = client.channelPath(projectId, locationId, channelId);

    // Initiates the long-running operation to delete the channel
    const [operation] = await client.deleteChannel({name});

    // Waits for the channel deletion operation to complete
    const [response] = await operation.promise();

    console.log(`Deleted channel: ${name}`);
    return response;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Channel not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else {
      console.error(`Failed to delete channel: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { deleteChannel };

Python

To authenticate to Eventarc, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import google.api_core.exceptions
from google.cloud import eventarc_v1

client = eventarc_v1.EventarcClient()


def delete_channel(project_id: str, location_id: str, channel_id: str) -> None:
    """Use the delete_channel method

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud region where the Eventarc channel is located.
        channel_id: The caller-chosen ID of the existing Channel to delete.
    """
    name = client.channel_path(project_id, location_id, channel_id)

    try:
        # DeleteChannel initiates a long-running operation
        operation = client.delete_channel(name=name)
        operation.result()
        print(f"Deleted channel: {name}")
    except google.api_core.exceptions.NotFound:
        print(f"Channel not found: {name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Request failed with error: {error}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.