Redacting sensitive data from text

Stay organized with collections Save and categorize content based on your preferences.

Cloud Data Loss Prevention can redact or obfuscate sensitive data from a string of text. You can feed textual information to the API using JSON over HTTP, or use one of the client libraries to do so using several popular programming languages.

The projects.content.deidentify API takes the following as arguments:

  • A string of text.
  • Placeholder text that will replace any sensitive data detected. In this example, the data is replaced with its corresponding infoType.
  • A list of one or more infoTypes that you want to redact.

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

Example text redaction

For more information about using JSON with the DLP API, see the JSON quickstart.


JSON input:

  "item": {
     "value":"My email is",
   "deidentifyConfig": {
          "transformations": [
              "primitiveTransformation": {
                "replaceWithInfoTypeConfig": {}
    "inspectConfig": {
      "infoTypes": {
        "name": "EMAIL_ADDRESS"


Cloud DLP returns the following after receiving the request:

JSON output:

    "value":"My email is [EMAIL_ADDRESS]"



To learn how to install and use the client library for Cloud DLP, see Cloud DLP client libraries.


public class DeIdentifyWithInfoType {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String textToInspect = "My email is";
    deIdentifyWithInfoType(projectId, textToInspect);

  public static void deIdentifyWithInfoType(String projectId, String textToRedact)
      throws IOException {
    // 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 "close" method on the client to safely clean up any remaining background resources.
    try (DlpServiceClient dlp = DlpServiceClient.create()) {
      // Specify the content to be inspected.
      ContentItem item = ContentItem.newBuilder().setValue(textToRedact).build();

      // Specify the type of info the inspection will look for.
      // See for complete list of info types
      InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build();
      InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build();
      // Specify replacement string to be used for the finding.
      ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig =
      // Define type of deidentification as replacement with info type.
      PrimitiveTransformation primitiveTransformation =
      // Associate deidentification type with info type.
      InfoTypeTransformation transformation =
      // Construct the configuration for the Redact request and list all desired transformations.
      DeidentifyConfig redactConfig =

      // Construct the Redact request to be sent by the client.
      DeidentifyContentRequest request =
              .setParent(LocationName.of(projectId, "global").toString())

      // Use the client to send the API request.
      DeidentifyContentResponse response = dlp.deidentifyContent(request);

      // Parse the response and process results
      System.out.println("Text after redaction: " + response.getItem().getValue());


To learn how to install and use the client library for Cloud DLP, see Cloud DLP client libraries.

def deidentify_with_replace_infotype(project, item, info_types):
    """Uses the Data Loss Prevention API to deidentify sensitive data in a
    string by replacing it with the info type.
        project: The Google Cloud project id to use as a parent resource.
        item: The string to deidentify (will be treated as text).
        info_types: A list of strings representing info types to look for.
            A full list of info type categories can be fetched from the API.
        None; the response from the API is printed to the terminal.

    # Import the client library

    # Instantiate a client
    dlp =

    # Convert the project id into a full resource id.
    parent = f"projects/{project}"

    # Construct inspect configuration dictionary
    inspect_config = {"info_types": [{"name": info_type} for info_type in info_types]}

    # Construct deidentify configuration dictionary
    deidentify_config = {
        "info_type_transformations": {
            "transformations": [
                {"primitive_transformation": {"replace_with_info_type_config": {}}}

    # Call the API
    response = dlp.deidentify_content(
            "parent": parent,
            "deidentify_config": deidentify_config,
            "inspect_config": inspect_config,
            "item": {"value": item},

    # Print out the results.

You can try this out yourself using the API Explorer embedded here.

Next steps

Redaction is one form of de-identification. To learn more about how to de-identify content, see De-identifying sensitive data in text content.