Skip to content

# Delete Flow and its Data

During development, you may want to completely remove a flow and all its data to make breaking changes, clean up test data, or start fresh after experimenting. This operation is destructive and should only be used in development environments.

Data Loss Warning

This permanently deletes all flow data including:

- All run history
- All queued and archived messages
- All task outputs
- All flow definitions

Never use this in production. Use versioned flows instead (e.g., `my_flow_v1`, `my_flow_v2`) to safely deploy changes while maintaining complete flow history.

## Using the Delete Function

Section titled “Using the Delete Function”

The delete function accepts a flow slug parameter and removes all associated data:

    pgflow.delete_flow_and_data(flow_slug TEXT)

Example usage in local development:

    -- Delete a specific flow and all its dataSELECT pgflow.delete_flow_and_data('analyze_website');

This deletes the flow definition, all runs, queued messages, and task outputs for the specified flow.

## Installing the Function

Section titled “Installing the Function”

Manual Installation Required

This function is not included in default pgflow migrations to prevent accidental deployment to production. It has been thoroughly unit tested, but you must manually add it to your development database using the SQL in the Delete Function section below.

Install by running the SQL directly in your database using `psql` or Supabase Studio.

## The Delete Function

Section titled “The Delete Function”

Run this SQL to install the delete function:

    /** * Deletes a flow and all its associated data. * WARNING: This is destructive and should only be used during development. * * @param flow_slug - The slug of the flow to delete */create or replace function pgflow.delete_flow_and_data(  flow_slug TEXT) returns void language plpgsql as $$BEGIN  -- Drop queue and archive table  PERFORM pgmq.drop_queue(delete_flow_and_data.flow_slug);
      -- Delete all associated data in the correct order  DELETE FROM pgflow.step_tasks WHERE step_tasks.flow_slug = delete_flow_and_data.flow_slug;  DELETE FROM pgflow.step_states WHERE step_states.flow_slug = delete_flow_and_data.flow_slug;  DELETE FROM pgflow.runs WHERE runs.flow_slug = delete_flow_and_data.flow_slug;  DELETE FROM pgflow.deps WHERE deps.flow_slug = delete_flow_and_data.flow_slug;  DELETE FROM pgflow.steps WHERE steps.flow_slug = delete_flow_and_data.flow_slug;  DELETE FROM pgflow.flows WHERE flows.flow_slug = delete_flow_and_data.flow_slug;
      RAISE NOTICE 'Flow % and all associated data has been deleted', delete_flow_and_data.flow_slug;END$$;

## After Deleting

Section titled “After Deleting”

Once you’ve deleted the flow:

1. You can compile and deploy a fresh version without conflicts
2. The flow slug becomes available for reuse
3. All historical data is permanently lost

## See Also

Section titled “See Also”
Version your flows Safe flow updates for production
Tune deployed flows Adjust production flow settings