Job löschen

Auf dieser Seite wird beschrieben, wie Sie einen Batchjob löschen.

Hinweis

Job löschen

Löschen Sie einen Job, wenn Sie ihn aus der Jobliste Ihres Projekts entfernen möchten und der Verlauf des Jobs nicht mehr benötigt wird. Wenn Sie einen Job vor oder während der Ausführung löschen, wird der Job abgebrochen.

Sie können einen Job über die Google Cloud Console, die gcloud CLI, die Batch API, Go, Java, Node.js oder Python löschen.

Console

So löschen Sie einen Job über die Google Cloud Console:

  1. Rufen Sie in der Google Cloud Console die Seite Jobliste auf.

    Zur Jobliste

  2. Klicken Sie auf den Namen des von Ihnen erstellten Jobs. Die Seite Jobdetails wird geöffnet.

  3. Klicken Sie auf Löschen.

  4. Geben Sie im Dialogfeld Batchjob löschen? Delete ein.

  5. Klicken Sie auf Löschen.

    Auf der Seite Jobliste wird angezeigt, dass der Job gelöscht wurde.

gcloud

Verwenden Sie zum Löschen eines Jobs mit der gcloud CLI den Befehl gcloud batch jobs delete.

gcloud batch jobs delete JOB_NAME --location LOCATION

Dabei gilt:

  • JOB_NAME: der Name des Jobs.
  • LOCATION: Der Standort des Jobs.

API

Zum Löschen eines Jobs mit der Batch API können Sie die jobs.delete-Methode verwenden:

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

Dabei gilt:

  • PROJECT_ID: Die Projekt-ID Ihres Projekts.
  • LOCATION: Der Standort des Jobs.
  • JOB_NAME: der Name des Jobs.

Einfach loslegen (Go)

Go

Weitere Informationen finden Sie in der Referenzdokumentation zur Batch Go API.

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: %v", 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: %v", err)
	}

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

	return nil
}

Java

Java

Weitere Informationen finden Sie in der Referenzdokumentation zur Batch Java API.


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

Weitere Informationen finden Sie in der Referenzdokumentation zur Batch Node.js API.

/**
 * 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

Weitere Informationen finden Sie in der Referenzdokumentation zur Batch Python API.

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

Nächste Schritte