Skip to content

# Delete Flow and its Data

During development, you may want to completely remove a flow and all its associated data to start fresh. This operation is destructive and should never be used in production.

Data Loss Warning

This permanently deletes all flow data including:

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

## When to Use This

Section titled “When to Use This”

This approach is useful when:

- You need to make breaking changes during development
- You want to clean up test data
- You’re iterating on flow structure and need a fresh start

For production environments, always use versioned flows instead (e.g., `my_flow_v1`, `my_flow_v2`) to safely deploy new versions while maintaining complete flow history.

## Using the Delete Function

Section titled “Using the Delete Function”

pgflow includes a delete function that accepts a flow slug parameter:

    pgflow.delete_flow_and_data(flow_slug TEXT)

Example:

    -- Delete a specific flowSELECT pgflow.delete_flow_and_data('your_flow_slug');

## Installing the Function

Section titled “Installing the Function”

To install this function, run the following SQL directly in your database using psql or Supabase Studio. This approach helps prevent accidentally deploying this destructive function to production.

Caution

This function is not yet included in the default pgflow migrations since pgflow is still in early development and I’m learning how users interact with it. It has been thoroughly unit tested, but you’ll need to manually add it to your project as shown in the Delete Function section below.

## 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
Update flow options Non-breaking configuration changes