删除作业

本页面介绍何时以及如何删除 Batch 作业。

删除作业后,当您 查看作业及其任务时显示的作业详细信息和历史记录将从 Batch 中移除。如果您想移除与作业关联的所有信息和资源 ,还需要从您启用的任何其他 Google Cloud 产品(例如 Pub/Sub 主题、 BigQuery 表或 Cloud Logging 日志)中删除项。

Google Cloud 会在作业取消、成功或失败 60 天后自动删除作业。在作业自动删除之前,您可以选择执行以下任一操作:

  • 导出作业信息:如果您想将作业中的信息保留 60 天以上,可以使用 Workflows 将作业信息导出到 BigQuery。如需了解详情,请参阅 导出作业信息

  • 删除作业:如本文档中所述,当您准备好从项目的作业列表中移除作业 并且不再需要作业的历史记录时,可以手动 删除作业。如果您在作业运行之前或运行期间删除作业,则该作业将被取消并删除。

准备工作

  1. 如果您之前未使用过 Batch,请查看 Batch 使用入门 ,并完成 项目和用户的前提条件以启用 Batch。
  2. 如需获得删除作业所需的权限,请让您的管理员向您授予项目的Batch Job Editor (roles/batch.jobsEditor) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

删除作业

您可以使用 Google Cloud 控制台、gcloud CLI、 Batch API、Go、Java、Node.js、Python 或 C++ 删除作业。

控制台

如需使用 Google Cloud 控制台删除作业,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往作业列表页面。

    前往作业列表

  2. 点击您创建的作业的名称。作业详情 页面随即打开。

  3. 点击 删除

  4. 要删除批量作业吗? 对话框中,对于 字段,输入 Delete

  5. 点击删除

    作业列表 页面会显示作业已删除。

gcloud

如需使用 gcloud CLI 删除作业,请使用 gcloud batch jobs delete 命令

gcloud batch jobs delete JOB_NAME --location LOCATION

替换以下内容:

  • JOB_NAME:作业的名称。
  • LOCATION:作业的位置

API

如需使用 Batch API 删除作业,请使用 jobs.delete 方法

DELETE https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs/JOB_NAME

替换以下内容:

  • PROJECT_ID:项目的 项目 ID
  • LOCATION:作业的位置
  • JOB_NAME:作业的名称。

Go

Go

如需了解详情,请参阅 Batch Go API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
)

// Deletes the specified job
func deleteJob(w io.Writer, projectID, region, jobName string) error {
	// projectID := "your_project_id"
	// region := "us-central1"
	// jobName := "some-job"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer batchClient.Close()

	req := &batchpb.DeleteJobRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/jobs/%s", projectID, region, jobName),
	}

	response, err := batchClient.DeleteJob(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to delete job: %w", err)
	}

	fmt.Fprintf(w, "Job deleted: %v\n", response)

	return nil
}

Java

Java

如需了解详情,请参阅 Batch Java API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证

import com.google.cloud.batch.v1.BatchServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteJob {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the region hosts the job.
    String region = "europe-central2";

    // The name of the job that you want to delete.
    String jobName = "JOB_NAME";

    deleteJob(projectId, region, jobName);
  }

  // Triggers the deletion of a Job.
  public static void deleteJob(String projectId, String region, String jobName)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `batchServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

      // Construct the parent path of the job.
      String name = String.format("projects/%s/locations/%s/jobs/%s", projectId, region, jobName);

      batchServiceClient.deleteJobAsync(name).get(5, TimeUnit.MINUTES);
      System.out.printf("Delete the job: %s", jobName);
    }
  }
}

Node.js

Node.js

如需了解详情,请参阅 Batch Node.js API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
/**
 * The region that hosts the job.
 */
// const region = 'us-central-1';
/**
 * The name of the job you want to delete.
 */
// const jobName = 'YOUR_JOB_NAME';

// Imports the Batch library
const batchLib = require('@google-cloud/batch');

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

async function callDeleteJob() {
  // Construct request
  const request = {
    name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
  };

  // Run request
  const [operation] = await batchClient.deleteJob(request);
  const [response] = await operation.promise();
  console.log(response);
}

await callDeleteJob();

Python

Python

如需了解详情,请参阅 Batch Python API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证

from google.api_core.operation import Operation

from google.cloud import batch_v1


def delete_job(project_id: str, region: str, job_name: str) -> Operation:
    """
    Triggers the deletion of a Job.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region hosts the job.
        job_name: the name of the job that you want to delete.

    Returns:
        An operation object related to the deletion. You can call `.result()`
        on it to wait for its completion.
    """
    client = batch_v1.BatchServiceClient()

    return client.delete_job(
        name=f"projects/{project_id}/locations/{region}/jobs/{job_name}"
    )

C++

C++

如需了解详情,请参阅 Batch C++ API 参考文档

如需向 Batch 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证

#include "google/cloud/batch/v1/batch_client.h"

  [](std::string const& project_id, std::string const& location_id,
     std::string const& job_id) {
    auto const name = "projects/" + project_id + "/locations/" + location_id +
                      "/jobs/" + job_id;
    google::cloud::batch::v1::DeleteJobRequest request;
    request.set_name(name);
    // Initialize a client and issue the request.
    auto client = google::cloud::batch_v1::BatchServiceClient(
        google::cloud::batch_v1::MakeBatchServiceConnection());
    auto future = client.DeleteJob(request);
    // Wait until the long-running operation completes.
    auto success = future.get();
    if (!success) throw std::move(success).status();
    std::cout << "Job " << name << " successfully deleted\n";
  }

后续步骤