刪除工作

本頁說明刪除批次工作時機和方式。

刪除工作後,查看工作及其工作時顯示的工作詳細資料和記錄會從 Batch 中移除。如果您想移除與工作相關的所有資訊和資源,請務必一併刪除已啟用Google Cloud 產品中的項目,例如 Pub/Sub 主題、BigQuery 表格或 Cloud Logging 記錄。

Google Cloud 會在工作取消、成功或失敗後 60 天自動刪除。在工作自動刪除前,您可以選擇執行下列任一操作:

  • 匯出工作資訊:如要將工作資訊保留超過 60 天,您可以使用工作流程將工作資訊匯出至 BigQuery。詳情請參閱「匯出工作資訊」。

  • 刪除工作:如本文所述,如果您準備從專案的工作清單中移除工作,且不再需要該工作的記錄,可以手動刪除工作。如果您在工作開始執行前或執行期間刪除工作,系統會取消並刪除該工作。

事前準備

  1. 如果您之前未使用過 Batch,請參閱「開始使用 Batch」,並完成專案和使用者的必要條件,啟用 Batch。
  2. 如要取得刪除工作所需的權限,請要求管理員為您授予專案的 批次工作編輯者 (roles/batch.jobsEditor) 身分與存取權管理角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

    您或許還可透過自訂角色或其他預先定義的角色取得必要權限。

刪除工作

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

控制台

如要使用 Google Cloud 控制台刪除工作,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Job list」(工作清單) 頁面。

    前往「Job list」(工作清單)

  2. 按一下您建立的工作名稱。「Job details」(工作詳細資料) 頁面隨即開啟。

  3. 按一下 「刪除」

  4. 在「Delete batch job?」對話方塊的欄位中輸入 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";
  }

後續步驟