Elimina ed esporta job

Questa pagina descrive come eliminare ed esportare i job batch.

Quando un job viene eliminato, i relativi dettagli e la cronologia vengono visualizzati visualizzare un lavoro e le sue attività vengano rimosse da Batch. Se vuoi rimuovere tutte le informazioni e le risorse associati a un job, devi eliminare anche elementi da qualsiasi I prodotti Google Cloud che hai abilitato, come gli argomenti Pub/Sub, tabelle BigQuery o log di Cloud Logging.

Google Cloud elimina automaticamente un job 60 giorni dopo l'annullamento (Anteprima), va a buon fine o non va a buon fine. Prima di un job viene automaticamente eliminato, puoi scegliere di eseguire una delle seguenti operazioni:

  • Esporta il job: se vuoi conservare le informazioni del job. per più di 60 giorni, puoi procedere in uno dei seguenti modi:

  • Eliminare un lavoro: come spiegato in questo documento, puoi eseguire manualmente elimina un job quando vuoi rimuoverlo dall'elenco dei job del tuo progetto e non hanno più bisogno della cronologia del job. Se elimini un job prima o durante viene eseguito, il job viene annullato.

Prima di iniziare

Elimina un job

Puoi eliminare un job utilizzando la console Google Cloud, gcloud CLI, API Batch, Go, Java, Node.js, Python o C++.

Console

Per eliminare un job utilizzando la console Google Cloud, segui questi passaggi:

  1. Nella console Google Cloud, vai alla pagina Elenco job.

    Vai all'elenco dei job

  2. Fai clic sul nome del job che hai creato. Si apre la pagina Dettagli job.

  3. Fai clic su Elimina.

  4. Nella finestra di dialogo Vuoi eliminare il job batch?, inserisci Delete nel campo.

  5. Fai clic su Elimina.

    La pagina Elenco job mostra che il job è stato eliminato.

gcloud

Per eliminare un job con gcloud CLI, utilizza Comando gcloud batch jobs delete.

gcloud batch jobs delete JOB_NAME --location LOCATION

Sostituisci quanto segue:

  • JOB_NAME: il nome del job.
  • LOCATION: la località del lavoro.

API

Per eliminare un job utilizzando l'API Batch, utilizza Metodo jobs.delete:

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

Sostituisci quanto segue:

  • PROJECT_ID: il valore ID progetto del tuo progetto.
  • LOCATION: la località del lavoro.
  • JOB_NAME: il nome del job.

Vai

Go

Per ulteriori informazioni, consulta API Go Batch documentazione di riferimento.

Per eseguire l'autenticazione in modalità batch, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.

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

Per ulteriori informazioni, consulta API Java Batch documentazione di riferimento.

Per eseguire l'autenticazione in modalità batch, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.

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

Per ulteriori informazioni, consulta API Node.js Batch documentazione di riferimento.

Per eseguire l'autenticazione in modalità batch, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.

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

Per ulteriori informazioni, consulta API Python Batch documentazione di riferimento.

Per eseguire l'autenticazione in modalità batch, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.

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++

Per ulteriori informazioni, consulta API C++ Batch documentazione di riferimento.

Per eseguire l'autenticazione in modalità batch, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.

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

Passaggi successivi