删除预留组

从 BigQuery Reservations API 中删除预留组。只有当预留组不包含任何子预留时,才能删除该预留组。

代码示例

Node.js

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 BigQuery Node.js API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

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

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Python 设置说明进行操作。 如需了解详情,请参阅 BigQuery Python API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

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.")

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器