删除和导出作业

本页面介绍了如何删除和导出批量作业。

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

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

  • 导出作业:如果希望将作业中的信息保留 60 天以上,可以执行以下任一操作:

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

准备工作

删除作业

您可以使用 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);
}

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";
  }

后续步骤