Listing and Deleting Intents

Here is an example of creating an intent in a Dialogflow agent. To delete intents, you must first list the intents to get the intent IDs.

Set up your GCP project and authentication

Create an agent

Import the example intents and entities

List intents

Protocol

To list the intents for your project, use the following curl command to access the intents endpoint. Replace project-name with the name of your Google Cloud project. The command uses the gcloud command-line interface from the Google Cloud SDK to get an authorization token for the command. For instructions on setting up authorization, see Quickstart.

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     "https://dialogflow.googleapis.com/v2/projects/project-name/agent/intents"

You should see a response similar to the following:

{
  "intents": [
    {
      "name": "projects/project-name/agent/intents/5b290a94-55d6-4074-96f4-9c4c4879c2bb",
      "displayName": "ListRooms",
      "priority": 500000,
      "action": "listRooms",
      "messages": [
        {
          "text": {
            "text": [
              "Here are the available rooms:"
            ]
          }
        }
      ]
    },
    {
      "name": "projects/project-name/agent/intents/b31cc83c-edfd-4493-8f06-bee4c2db9d48",
      "displayName": "room.reservation - yes",
      "priority": 500000,
      "inputContextNames": [
        "projects/project-name/agent/sessions/-/contexts/roomreservation-chooseroom-followup"
      ],
      "action": "room.reservation.yes",
      "messages": [
        {
          "text": {
            "text": [
              "All set!"
            ]
          }
        }
      ],
      "defaultResponsePlatforms": [
        "FACEBOOK"
      ],
      "rootFollowupIntentName": "projects/project-name/agent/intents/e8f6a63e-73da-4a1a-8bfc-857183f71228",
      "parentFollowupIntentName": "projects/project-name/agent/intents/3d235a1b-5de9-4e1b-b5d4-6af6986e5544"
    },
    {
      "name": "projects/project-name/agent/intents/c7bf7181-a73f-4763-bb52-eae7c20df3c8",
      "displayName": "room.reservation - cancel",
      "priority": 500000,
      "inputContextNames": [
        "projects/project-name/agent/sessions/-/contexts/roomreservation-chooseroom-followup"
      ],
      "action": "room.reservation.cancel",
      "messages": [
        {
          "text": {
            "text": [
              "Okay. Cancelled."
            ]
          }
        }
      ],
      "defaultResponsePlatforms": [
        "FACEBOOK"
      ],
      "rootFollowupIntentName": "projects/project-name/agent/intents/e8f6a63e-73da-4a1a-8bfc-857183f71228",
      "parentFollowupIntentName": "projects/project-name/agent/intents/3d235a1b-5de9-4e1b-b5d4-6af6986e5544"
    },
  ]
}

C#

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

        public static int List(string projectId)
        {
            var client = IntentsClient.Create();

            var intents = client.ListIntents(new ProjectAgentName(projectId));

            foreach (var intent in intents)
            {
                Console.WriteLine($"Intent name: {intent.Name}");
                Console.WriteLine($"Intent display name: {intent.DisplayName}");
                Console.WriteLine($"Action: {intent.Action}");
                Console.WriteLine($"Root follow-up intent: {intent.RootFollowupIntentName}");
                Console.WriteLine($"Parent follow-up intent: {intent.ParentFollowupIntentName}");

                Console.WriteLine($"Input contexts:");
                foreach (var inputContextName in intent.InputContextNames)
                {
                    Console.WriteLine($"Input context name: {inputContextName}");
                }

                Console.WriteLine($"Output contexts:");
                foreach (var outputContex in intent.OutputContexts)
                {
                    Console.WriteLine($"Output context name: {outputContex.Name}");
                }
                Console.WriteLine("Messages:");
                foreach (var message in intent.Messages)
                {
                    if (message.Text != null)
                    {
                        foreach (var text in message.Text.Text_)
                        {
                            Console.WriteLine($"Message text: {text}");
                        }
                    }
                }
                Console.WriteLine($"Training Phrases ({intent.TrainingPhrases.Count})");
                foreach (var trainingPhrase in intent.TrainingPhrases)
                {
                    Console.WriteLine($"Phrase name: {trainingPhrase.Name}");
                    Console.WriteLine($"Phrase type: {trainingPhrase.Type}");
                    foreach (var phrasePart in trainingPhrase.Parts)
                    {
                        Console.WriteLine($"Phrase part: {phrasePart.Text}");
                    }
                }
                Console.WriteLine();
            }

            return 0;
        }

Go

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

func ListIntents(projectID string) ([]*dialogflowpb.Intent, error) {
	ctx := context.Background()

	intentsClient, clientErr := dialogflow.NewIntentsClient(ctx)
	if clientErr != nil {
		return nil, clientErr
	}
	defer intentsClient.Close()

	if projectID == "" {
		return nil, errors.New(fmt.Sprintf("Received empty project (%s)", projectID))
	}

	parent := fmt.Sprintf("projects/%s/agent", projectID)

	request := dialogflowpb.ListIntentsRequest{Parent: parent}

	intentIterator := intentsClient.ListIntents(ctx, &request)
	var intents []*dialogflowpb.Intent

	for intent, status := intentIterator.Next(); status != iterator.Done; {
		intents = append(intents, intent)
		intent, status = intentIterator.Next()
	}

	return intents, nil
}

Java

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

/**
 * List intents
 * @param projectId Project/Agent Id.
 */
public static void listIntents(String projectId) throws Exception {
  // Instantiates a client
  try (IntentsClient intentsClient = IntentsClient.create()) {
    // Set the project agent name using the projectID (my-project-id)
    ProjectAgentName parent = ProjectAgentName.of(projectId);

    // Performs the list intents request
    for (Intent intent : intentsClient.listIntents(parent).iterateAll()) {
      System.out.println("====================");
      System.out.format("Intent name: '%s'\n", intent.getName());
      System.out.format("Intent display name: '%s'\n", intent.getDisplayName());
      System.out.format("Action: '%s'\n", intent.getAction());
      System.out.format("Root followup intent: '%s'\n", intent.getRootFollowupIntentName());
      System.out.format("Parent followup intent: '%s'\n", intent.getParentFollowupIntentName());

      System.out.format("Input contexts:\n");
      for (String inputContextName : intent.getInputContextNamesList()) {
        System.out.format("\tName: %s\n", inputContextName);
      }

      System.out.format("Output contexts:\n");
      for (Context outputContext : intent.getOutputContextsList()) {
        System.out.format("\tName: %s\n", outputContext.getName());
      }
    }
  }
}

PHP

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

namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\IntentsClient;

function intent_list($projectId)
{
    // get intents
    $intentsClient = new IntentsClient();
    $parent = $intentsClient->projectAgentName($projectId);
    $intents = $intentsClient->listIntents($parent);

    foreach ($intents->iterateAllElements() as $intent) {
        // print relevant info
        print(str_repeat("=", 20) . PHP_EOL);
        printf('Intent name: %s' . PHP_EOL, $intent->getName());
        printf('Intent display name: %s' . PHP_EOL, $intent->getDisplayName());
        printf('Action: %s' . PHP_EOL, $intent->getAction());
        printf('Root followup intent: %s' . PHP_EOL,
            $intent->getRootFollowupIntentName());
        printf('Parent followup intent: %s' . PHP_EOL,
            $intent->getParentFollowupIntentName());
        print(PHP_EOL);

        print('Input contexts: ' . PHP_EOL);
        foreach ($intent->getInputContextNames() as $inputContextName) {
            printf("\t Name: %s" . PHP_EOL, $inputContextName);
        }

        print('Output contexts: ' . PHP_EOL);
        foreach ($intent->getOutputContexts() as $outputContext) {
            printf("\t Name: %s" . PHP_EOL, $outputContext->getName());
        }
    }
    $intentsClient->close();
}

Python

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

def list_intents(project_id):
    intents_client = dialogflow.IntentsClient()

    parent = intents_client.project_agent_path(project_id)

    intents = intents_client.list_intents(parent)

    for intent in intents:
        print('=' * 20)
        print('Intent name: {}'.format(intent.name))
        print('Intent display_name: {}'.format(intent.display_name))
        print('Action: {}\n'.format(intent.action))
        print('Root followup intent: {}'.format(
            intent.root_followup_intent_name))
        print('Parent followup intent: {}\n'.format(
            intent.parent_followup_intent_name))

        print('Input contexts:')
        for input_context_name in intent.input_context_names:
            print('\tName: {}'.format(input_context_name))

        print('Output contexts:')
        for output_context in intent.output_contexts:
            print('\tName: {}'.format(output_context.name))

Ruby

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

# project_id = "Your Google Cloud project ID"

require "google/cloud/dialogflow"

intents_client = Google::Cloud::Dialogflow::Intents.new
parent = intents_client.class.project_agent_path project_id

intents = intents_client.list_intents(parent)

intents.each do |intent|
  puts "Intent name:            #{intent.name}"
  puts "Intent display name:    #{intent.display_name}"
  puts "Action:                 #{intent.action}"
  puts "Root followup intent:   #{intent.root_followup_intent_name}"
  puts "Parent followup intent: #{intent.parent_followup_intent_name}"

  puts("Input contexts:")
  intent.input_context_names.each do |input_context_name|
    puts "\tName: #{input_context_name}"
  end

  puts("Output contexts:")
  intent.output_contexts.each do |output_context|
    puts "\tName: #{output_context.name}"
  end
end

Delete Intents

Protocol

To delete an intent from your project, use the following curl command to send a DELETE command to the intents endpoint. Replace project-name with the name of your Google Cloud project and replace the sample intent id (5b290a94-55d6-4074-96f4-9c4c4879c2bb) with the intent id of the intent that you want to delete.

curl -X DELETE -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
               -H "Content-Type: application/json; charset=utf-8" \
               "https://dialogflow.googleapis.com/v2/projects/project-name/agent/intents/5b290a94-55d6-4074-96f4-9c4c4879c2bb"

You should see an empty response.

C#

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

        public static int Delete(string projectId, string intentId)
        {
            var client = IntentsClient.Create();

            client.DeleteIntent(new IntentName(projectId, intentId));

            Console.WriteLine($"Deleted Intent: {intentId}");

            return 0;
        }

Go

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

func DeleteIntent(projectID, intentID string) error {
	ctx := context.Background()

	intentsClient, clientErr := dialogflow.NewIntentsClient(ctx)
	if clientErr != nil {
		return clientErr
	}
	defer intentsClient.Close()

	if projectID == "" || intentID == "" {
		return errors.New(fmt.Sprintf("Received empty project (%s) or intent (%s)", projectID, intentID))
	}

	targetPath := fmt.Sprintf("projects/%s/agent/intents/%s", projectID, intentID)

	request := dialogflowpb.DeleteIntentRequest{Name: targetPath}

	requestErr := intentsClient.DeleteIntent(ctx, &request)
	if requestErr != nil {
		return requestErr
	}

	return nil
}

Java

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

/**
 * Delete intent with the given intent type and intent value
 * @param intentId The id of the intent.
 * @param projectId Project/Agent Id.
 */
public static void deleteIntent(String intentId, String projectId) throws Exception {
  // Instantiates a client
  try (IntentsClient intentsClient = IntentsClient.create()) {
    IntentName name = IntentName.of(projectId, intentId);
    // Performs the delete intent request
    intentsClient.deleteIntent(name);
  }
}

Node.js

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

// Imports the Dialogflow library
const dialogflow = require('dialogflow');

// Instantiates clients
const intentsClient = new dialogflow.IntentsClient();

const request = {name: intent.name};

// Send the request for retrieving the intent.
return intentsClient
  .deleteIntent(request)
  .then(() => {
    console.log(`Intent ${intent.displayName} deleted`);
  })
  .catch(err => {
    console.error(`Failed to delete intent ${intent.displayName}:`, err);
  });

PHP

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

namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\IntentsClient;

/**
* Delete intent with the given intent type and intent value.
*/
function intent_delete($projectId, $intentId)
{
    $intentsClient = new IntentsClient();
    $intentName = $intentsClient->intentName($projectId, $intentId);

    $intentsClient->deleteIntent($intentName);
    printf('Intent deleted: %s' . PHP_EOL, $intentName);

    $intentsClient->close();
}

Python

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

def delete_intent(project_id, intent_id):
    """Delete intent with the given intent type and intent value."""
    intents_client = dialogflow.IntentsClient()

    intent_path = intents_client.intent_path(project_id, intent_id)

    intents_client.delete_intent(intent_path)

Ruby

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

# project_id = "Your Google Cloud project ID"
# intent_id = "Existing Intent ID"

require "google/cloud/dialogflow"

intents_client = Google::Cloud::Dialogflow::Intents.new
intent_path = intents_client.class.intent_path project_id, intent_id

intents_client.delete_intent intent_path

puts "Deleted Intent: #{intent_id}"
nt.py" region_tag="dialogflow_delete_intent" %}

Was this page helpful? Let us know how we did:

Send feedback about...

Dialogflow Enterprise Edition Documentation