Delete a reservation group

Deletes a reservation group from the BigQuery Reservation API. A reservation group can only be deleted if it does not contain any child reservations.

Code sample

Java

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

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

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigquery.reservation.v1.DeleteReservationGroupRequest;
import com.google.cloud.bigquery.reservation.v1.ReservationGroupName;
import com.google.cloud.bigquery.reservation.v1.ReservationServiceClient;
import java.io.IOException;

public class DeleteReservationGroup {

  /**
   * Deletes a specified BigQuery reservation group. A reservation group cannot be deleted if it
   * contains any reservations.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param location The geographic location of the reservation group (for example, "us-central1").
   * @param reservationGroupId The ID of the reservation group to delete (for example,
   *     "example-reservation-group").
   */
  public static void deleteReservationGroup(
      String projectId, String location, String reservationGroupId) {
    ReservationGroupName name =
        ReservationGroupName.of(projectId, location, reservationGroupId);

    DeleteReservationGroupRequest request =
        DeleteReservationGroupRequest.newBuilder().setName(name.toString()).build();

    try (ReservationServiceClient client = ReservationServiceClient.create()) {
      client.deleteReservationGroup(request);
      System.out.printf("Deleted reservation group: '%s'%n", name);
    } catch (NotFoundException err) {
      System.err.printf(
          "error: reservation group '%s' not found in location '%s' for project '%s': %s%n",
          reservationGroupId, location, projectId, err.getMessage());
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to delete reservation group '%s' in location '%s' for project '%s': %s%n",
          reservationGroupId, location, projectId, err.getMessage());
    }
  }

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 {ReservationServiceClient} =
  require('@google-cloud/bigquery-reservation').v1;
const {status} = require('@grpc/grpc-js');

const client = new ReservationServiceClient();

/**
 * Deletes the specified reservation group.
 * A reservation group can only be deleted if it contains no reservations.
 * @param {string} projectId Google Cloud Project ID, for example 'example-project-id'.
 * @param {string} location Google Cloud Location, for example 'us-central1'.
 * @param {string} reservationGroupId The ID of the reservation group to delete, for example 'example-reservation-group'.
 */
async function deleteReservationGroup(
  projectId,
  location = 'us-central1',
  reservationGroupId = 'example-group-reservation',
) {
  const request = {
    name: client.reservationGroupPath(projectId, location, reservationGroupId),
  };

  try {
    await client.deleteReservationGroup(request);
    console.log(`Deleted reservation group: ${reservationGroupId}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(
        `Reservation group ${reservationGroupId} does not exist in location ${location} of project ${projectId}.`,
      );
    } else {
      console.error(
        `Error deleting reservation group ${reservationGroupId}:`,
        err,
      );
    }
  }
}

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.

from google.api_core.exceptions import NotFound
from google.cloud import bigquery_reservation_v1


client = bigquery_reservation_v1.ReservationServiceClient()


def delete_reservation_group(project_id: str, location: str, reservation_group_id: str):
    """Deletes a reservation group.

    Note that a reservation group cannot be deleted if it contains any
    reservations.

    Args:
        project_id: The Google Cloud project ID.
        location: The geographic location of the reservation group, for example, us-central1.
        reservation_group_id: The ID of the reservation group to delete.
    """
    name = client.reservation_group_path(project_id, location, reservation_group_id)

    try:
        client.delete_reservation_group(name=name)
        print(f"Deleted reservation group: '{name}'")
    except NotFound:
        print(f"Reservation group '{name}' not found.")

What's next

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