Cancel job (v2)

Cancel a Data Loss Prevention job.

Code sample

Java

To learn how to install and use the client library for Sensitive Data Protection, see Sensitive Data Protection client libraries.

To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.CancelDlpJobRequest;
import java.io.IOException;

public class CancelDlpJob {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project_id";
    String dlpJobId = "example-dlp_job_id";
    cancelDlpJob(projectId, dlpJobId);
  }

  public static void cancelDlpJob(String projectId, String dlpJobId) throws Exception {
    try (DlpServiceClient client = DlpServiceClient.create()) {
      String name = String.format("projects/%s/dlpJobs/%s", projectId, dlpJobId);

      CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(name).build();
      client.cancelDlpJob(request);
      System.out.printf("Canceled DLP job: %s%n", name);
    }
  }
}

Node.js

To learn how to install and use the client library for Sensitive Data Protection, see Sensitive Data Protection client libraries.

To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {DlpServiceClient} = require('@google-cloud/dlp');
const {status} = require('@grpc/grpc-js');

const client = new DlpServiceClient();

/**
 * Cancels a Cloud DLP job.
 *
 * @param {string} projectId - The Google Cloud project ID that owns the DLP job.
 * @param {string} dlpJobId - The server-generated ID of the DLP job to cancel.
 */
async function cancelDlpJob(projectId, dlpJobId) {
  try {
    const name = `projects/${projectId}/dlpJobs/${dlpJobId}`;

    const request = {
      name,
    };

    await client.cancelDlpJob(request);
    console.log(`Job canceled successfully: ${name}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`DLP job not found: ${err.message}`);
    } else if (err.code === status.FAILED_PRECONDITION) {
      console.error(`Job cannot be canceled in its current state: ${err.message}`);
    } else {
      console.error(`API error canceling DLP job: ${err.message}`);
    }
  }
}

module.exports = { cancelDlpJob };

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.