Delete a BigQuery connection

Deletes a specified BigQuery connection.

Code sample

Node.js

Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

const {ConnectionServiceClient} =
  require('@google-cloud/bigquery-connection').v1;
const {status} = require('@grpc/grpc-js');

const client = new ConnectionServiceClient();

/**
 * Deletes a connection and its associated credentials.
 *
 * @param {string} projectId Google Cloud project ID (for example, 'example-project-id').
 * @param {string} location The location where the connection resides (for example, 'us-central1').
 * @param {string} connectionId The ID of the connection to delete (for example, 'example-connection').
 */
async function deleteConnection(projectId, location, connectionId) {
  const request = {
    name: client.connectionPath(projectId, location, connectionId),
  };

  try {
    await client.deleteConnection(request);
    console.log(`Connection ${connectionId} deleted successfully.`);
  } catch (error) {
    if (error.code === status.NOT_FOUND) {
      console.log(
        `Connection ${connectionId} does not exist in location ${location} of project ${projectId}.`,
      );
    } else {
      console.error(`Error deleting connection ${connectionId}:`, error);
    }
  }
}

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

import google.api_core.exceptions
from google.cloud import bigquery_connection_v1

client = bigquery_connection_v1.ConnectionServiceClient()


def delete_connection(project_id: str, location: str, connection_id: str):
    """Deletes a BigQuery connection.

    Args:
        project_id: The Google Cloud project ID.
        location: Location of the connection (for example, "us-central1").
        connection_id: ID of the connection to delete.
    """
    name = client.connection_path(project_id, location, connection_id)

    try:
        client.delete_connection(name=name)
        print(f"Connection '{connection_id}' was deleted.")
    except google.api_core.exceptions.NotFound:
        print(f"Connection '{connection_id}' not found.")

What's next

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