Redacting Sensitive Data From Text Content

You can use the code samples below to redact sensitive data from a string of text. You can also feed information to the API using JSON over HTTP.

The API takes the following as arguments:

  • A string of text
  • The placeholder text that will replace sensitive data (in this example, "REDACTED")
  • A list of one or more InfoTypes that you want to redact

It returns the string with any sensitive data replaced by your chosen placeholder.



See the JSON quickstart for more information on using JSON.

Sample Input:

      "value": "My phone number is (123) 456-7890",
      "type": "text/plain"
      "replaceWith": "[REDACTED PHONE NUMBER]",
        "name": "PHONE_NUMBER"


Sample Output:

      "type": "text/plain",
      "value": "My phone number is [REDACTED PHONE NUMBER]"


For more on installing and creating a DLP API client, refer to DLP API Client Libraries.

// Instantiate the DLP client
try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
  // The minimum likelihood required before returning a match
  // eg.minLikelihood = LIKELIHOOD_VERY_LIKELY;
  InspectConfig inspectConfig =

  ContentItem contentItem =

  List<ReplaceConfig> replaceConfigs = new ArrayList<>();

  if (infoTypes.isEmpty()) {
    // replace all detected sensitive elements with replacement string
  } else {
    // Replace select info types with chosen replacement string
    for (InfoType infoType : infoTypes) {

  RedactContentResponse contentResponse =
          inspectConfig, Collections.singletonList(contentItem), replaceConfigs);
  for (ContentItem responseItem : contentResponse.getItemsList()) {
    // print out string with redacted content


For more on installing and creating a DLP API client, refer to DLP API Client Libraries.

// Imports the Google Cloud Data Loss Prevention library
const DLP = require('@google-cloud/dlp');

// Instantiates a client
const dlp = DLP();

// The string to inspect
// const string = 'My name is Gary and my email is';

// The string to replace sensitive data with
// const replaceString = 'REDACTED';

// The minimum likelihood required before redacting a match
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';

// The infoTypes of information to redact
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];

const items = [{ type: 'text/plain', value: string }];

const replaceConfigs = => {
  return {
    infoType: infoType,
    replaceWith: replaceString

const request = {
  inspectConfig: {
    infoTypes: infoTypes,
    minLikelihood: minLikelihood
  items: items,
  replaceConfigs: replaceConfigs

  .then((body) => {
    const results = body[0].items[0].value;
  .catch((err) => {
    console.log(`Error in redactString: ${err.message || err}`);


For more on installing and creating a DLP API client, refer to DLP API Client Libraries.

use Google\Cloud\Dlp\V2beta1\DlpServiceClient;
use Google\Privacy\Dlp\V2beta1\ContentItem;
use Google\Privacy\Dlp\V2beta1\InfoType;
use Google\Privacy\Dlp\V2beta1\InspectConfig;
use Google\Privacy\Dlp\V2beta1\RedactContentRequest_ReplaceConfig;
use Google\Privacy\Dlp\V2beta1\Likelihood;

 * Redact a sensitive string using the Data Loss Prevention (DLP) API.
 * @param string $string The text to inspect
function redact_string(
    $minLikelihood = likelihood::LIKELIHOOD_UNSPECIFIED,
    $maxFindings = 0)
    // Instantiate a client.
    $dlp = new DlpServiceClient();

    // The infoTypes of information to match
    $usMaleNameInfoType = new InfoType();
    $usFemaleNameInfoType = new InfoType();
    $infoTypes = [$usMaleNameInfoType, $usFemaleNameInfoType];

    // Whether to include the matching string in the response
    $includeQuote = true;

    // Create the configuration object
    $inspectConfig = new InspectConfig();

    $content = new ContentItem();

    $redactConfigs = [];
    foreach ($infoTypes as $infoType) {
        $redactConfig = new RedactContentRequest_ReplaceConfig();
        $redactConfigs[] = $redactConfig;

    // Run request
    $response = $dlp->redactContent($inspectConfig, [$content], $redactConfigs);
    $content = $response->getItems()[0];

    // Print the results
    print('Redacted String: ' . $content->getValue() . PHP_EOL);

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Data Loss Prevention API