# `Gust.Run.Pruner`
[🔗](https://github.com/marciok/gust/blob/v0.1.43/lib/gust/run/pruner.ex#L1)

Deletes old finished runs of DAGs that set a run retention.

A DAG opts in with `runs: [retention: [max_count: n]]`: its `n` most recent
finished (`:succeeded` or `:failed`) runs are kept and older ones are
deleted, together with their tasks and logs. Unfinished runs are never
deleted and don't count toward `n`. DAGs without a retention keep all runs.

Pruning runs on a schedule only when the app configures one:

    config :gust, :run_pruner, schedule: "@daily", deletion_batch_size: 200

  * `schedule` - a crontab expression, including shortcuts such as
    `@hourly` or `@daily`. `Gust.DAG.Run.Cron.Scheduler` registers the job,
    so it runs on the leader only and never overlaps itself. Without it
    nothing is deleted, unless `prune/3` is called directly (e.g. by the
    CLI).
  * `deletion_batch_size` - runs deleted per statement. Defaults to
    200; anything but a positive integer uses the
    default.

Runs are deleted in batches, each one a single short statement, and every
batch broadcasts `Gust.PubSub.broadcast_runs_pruned/2` so open dashboards
drop the deleted runs. The number of runs deleted per DAG is logged.

Deleting rows doesn't shrink tables on disk: autovacuum makes the space
reusable, so tables stop growing. Use `VACUUM FULL` or `pg_repack` once to
reclaim space on instances that are already large.

# `configured_schedule`

```elixir
@spec configured_schedule() :: term()
```

Returns the `schedule` as configured, or `nil` when it isn't set.

# `deletion_batch_size`

```elixir
@spec deletion_batch_size() :: pos_integer()
```

Returns the configured `deletion_batch_size`, or the default of
200 when it's unset or not a positive integer.

# `prune`

```elixir
@spec prune(integer(), pos_integer(), keyword()) :: non_neg_integer()
```

Deletes the finished runs of a DAG beyond the `max_count` most recent ones
and returns how many were deleted.

## Options

  * `:dry_run` - when `true`, deletes nothing and returns how many runs
    would be deleted. Defaults to `false`.

# `prune_all`

```elixir
@spec prune_all() :: %{required(String.t()) =&gt; non_neg_integer()}
```

Prunes every loaded DAG that sets a retention.

Returns a map of DAG name to the number of runs deleted.

# `retained_dags`

```elixir
@spec retained_dags() :: [
  %{dag_id: integer(), name: String.t(), max_count: pos_integer()}
]
```

Lists the loaded DAGs that set a retention, sorted by name.

# `schedule`

```elixir
@spec schedule() ::
  {:ok, Crontab.CronExpression.t()} | :disabled | {:error, String.t()}
```

Returns the configured `schedule`, parsed.

Returns `:disabled` when it isn't configured, and an error for an invalid
expression or `@reboot`, which would only prune when the Scheduler starts.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
