ジョブの削除とエクスポート

このページでは、Batch ジョブを削除およびエクスポートする方法について説明します。

ジョブが削除されると、ジョブとタスクを表示したときに表示されるジョブの詳細と履歴が Batch から削除されます。ジョブに関連するすべての情報とリソースを削除する場合は、Pub/Sub トピック、BigQuery テーブル、Cloud Logging ログなど、有効にした追加の Google Cloud プロダクトからもアイテムを削除する必要があります。

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

以下を置き換えます。

API

Batch API を使用してジョブを削除するには、jobs.delete メソッドを使用します。

DELETE https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs/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";
  }

次のステップ