Ora

How do you delete a function in Pulsar?

Published in Pulsar Function Management 4 mins read

To delete a Pulsar function, you can leverage the Pulsar Admin CLI, REST API, or Java Admin API. The most common and direct approach involves using the delete subcommand via the Admin CLI.

Understanding Pulsar Function Deletion

Deleting a Pulsar function is a necessary administrative task when a function is no longer needed, is being replaced, or when cleaning up resources. Pulsar provides robust interfaces to manage the lifecycle of your functions, including their removal from the cluster.

You have three primary methods for deleting functions:

  1. Pulsar Admin CLI: Ideal for manual operations, scripting, and command-line automation.
  2. REST API: Best suited for programmatic deletion from web services or other applications.
  3. Java Admin API: Designed for integrating function management directly into Java-based applications.

Deleting a Pulsar Function Using the Admin CLI

The Pulsar Admin Command Line Interface (CLI) is the most straightforward method to delete a function. It uses the pulsar-admin functions delete subcommand.

Syntax for Admin CLI Deletion

The general syntax requires you to specify the tenant, namespace, and name of the function you wish to delete:

pulsar-admin functions delete \
  --tenant <tenant-name> \
  --namespace <namespace-name> \
  --name <function-name>
  • --tenant <tenant-name>: The name of the tenant where the function is deployed.
  • --namespace <namespace-name>: The namespace within the tenant where the function resides.
  • --name <function-name>: The unique name of the function to be deleted.

Practical Example with Admin CLI

Let's say you have a function named my-data-processor deployed in the public tenant and default namespace. To delete it, you would execute:

pulsar-admin functions delete --tenant public --namespace default --name my-data-processor

Upon successful execution, the function will be removed from the Pulsar cluster, and its associated resources will be deallocated.

Practical Considerations

  • Verification: Always double-check the function's tenant, namespace, and name before executing the delete command to avoid accidental deletion of the wrong function.
  • Permissions: Ensure that your Pulsar Admin CLI configuration has the necessary permissions to delete functions in the specified tenant and namespace.
  • Connectivity: Confirm that your Admin CLI is properly configured to connect to your Pulsar cluster.

Deleting a Pulsar Function Using the REST API

For programmatic control over function deletion, the Pulsar REST API offers a direct endpoint. This is commonly used in automated scripts, CI/CD pipelines, or custom management tools.

REST API Endpoint

To delete a function via the REST API, you send an HTTP DELETE request to the following endpoint:

DELETE /admin/v3/functions/{tenant}/{namespace}/{functionName}
  • {tenant}: The tenant name.
  • {namespace}: The namespace name.
  • {functionName}: The name of the function.

Example with cURL (REST API)

Using curl to interact with the REST API, you can delete the my-data-processor function:

curl -X DELETE http://localhost:8080/admin/v3/functions/public/default/my-data-processor

Replace http://localhost:8080 with the actual HTTP service URL of your Pulsar cluster's broker.

Deleting a Pulsar Function Using the Java Admin API

The Java Admin API provides a robust way to manage Pulsar functions directly within Java applications. This is useful for building custom tools or integrating function lifecycle management into your application's logic.

Java Admin API Code Snippet

Here's a basic Java example demonstrating how to delete a function:

import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;

public class FunctionDeleter {

    public static void main(String[] args) {
        String webServiceUrl = "http://localhost:8080"; // Replace with your Pulsar HTTP service URL
        String tenant = "public";
        String namespace = "default";
        String functionName = "my-data-processor";

        try (PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(webServiceUrl).build()) {
            admin.functions().deleteFunction(tenant, namespace, functionName);
            System.out.println("Pulsar function '" + functionName + "' in tenant '" + tenant + "', namespace '" + namespace + "' deleted successfully.");
        } catch (PulsarAdminException e) {
            System.err.println("Error deleting Pulsar function: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("An unexpected error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

This code snippet initializes a PulsarAdmin client, then calls the deleteFunction method on the functions() interface, specifying the function's tenant, namespace, and name.

Summary of Deletion Methods

Here's a quick overview of the methods to delete a Pulsar function:

Method Use Case Command/Endpoint
Admin CLI Manual operations, scripting pulsar-admin functions delete --tenant <t> --namespace <n> --name <f>
REST API Programmatic deletion DELETE /admin/v3/functions/{tenant}/{namespace}/{functionName}
Java Admin API Embedded in Java applications admin.functions().deleteFunction(tenant, namespace, functionName)

For more detailed information on managing Pulsar functions, refer to the official Apache Pulsar documentation.