Delete workflow (v1)

Delete an existing workflow.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Go API reference documentation.

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


import (
	"context"
	"fmt"
	"io"

	workflows "cloud.google.com/go/workflows/apiv1beta"
	workflowspb "cloud.google.com/go/workflows/apiv1beta/workflowspb"
)

// deleteWorkflow deletes a workflow using the DeleteWorkflow method.
func deleteWorkflow(ctx context.Context, w io.Writer, projectID string, locationID string, workflowID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/workflows/%s", projectID, locationID, workflowID)

	client, err := workflows.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient failed: %w", err)
	}
	defer client.Close()

	req := &workflowspb.DeleteWorkflowRequest{
		Name: name,
	}

	op, err := client.DeleteWorkflow(ctx, req)
	if err != nil {
		return fmt.Errorf("DeleteWorkflow: %w", err)
	}

	if err := op.Wait(ctx); err != nil {
		return fmt.Errorf("Wait: %w", err)
	}

	fmt.Fprintf(w, "Deleted workflow: %s\n", name)
	return nil
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Node.js API reference documentation.

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

'use strict';

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

const client = new WorkflowsClient();

/**
 * Use the deleteWorkflow method
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 * @param {string} workflowId - ID of the Workflow (for example, 'example-workflow-id')
 */
async function deleteWorkflow(projectId, locationId, workflowId) {
  try {
    const name = client.workflowPath(projectId, locationId, workflowId);
    const [operation] = await client.deleteWorkflow({name});
    await operation.promise();
    console.log(`Deleted workflow: ${name}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Workflow not found: ${projectId}/${locationId}/${workflowId}`);
    } else {
      console.error(`Error deleting workflow: ${err.message}`);
    }
  }
}

module.exports = { deleteWorkflow };

Python

Before trying this sample, follow the Python setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Python API reference documentation.

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

import google.api_core.exceptions
from google.cloud import workflows_v1

client = workflows_v1.WorkflowsClient()


def delete_workflow(project_id: str, location_id: str, workflow_id: str) -> None:
    """Deletes an existing workflow.

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id')
        location_id: The Google Cloud region where the service is located (for example, 'us-central1')
        workflow_id: ID of the Workflow (for example, 'example-workflow-id')
    """
    name = client.workflow_path(project_id, location_id, workflow_id)

    try:
        operation = client.delete_workflow(name=name)
        print(f"Waiting for deletion operation to complete: {operation.operation.name}")
        operation.result()
        print(f"Deleted workflow: {name}")
    except google.api_core.exceptions.NotFound:
        print(f"Workflow not found: {name}")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(f"Failed to delete workflow: {e}")

What's next

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