# Sweeping idle local platforms and old builds on a schedule

> The cleanup a schedule can run unattended: it stops and removes idle test platforms and other throwaway local platforms, cached CLI builds nothing uses, and old publish caches — and keeps everything it cannot prove idle.

<!-- id: local-pruning-local-leftovers · area: local · stability: stable · html: https://osysharp.com/reference/local/pruning-local-leftovers/ -->

## Summary        {#summary}

A machine that runs many test runs — a build host, an agent's box — collects three kinds of leftovers: test platforms
that stay up long after their last run, each with its own database; cached CLI builds (about 730 MB each); and publish
caches. `osy instances prune` removes the ones nothing is using. It is a dry run unless you pass `--apply`, so it can
be read first and then put on a schedule.

## Signature      {#signature}

```console
osy instances prune [--apply] [--idle-hours <n>] [--cache-days <n>] [--keep-builds <n>] [--json]
```

## Description    {#description}

**On its own it changes nothing.** It prints each item it would remove, why, and its size, then everything it keeps
with the reason. `--apply` does it. `--json` prints the same plan (and, with `--apply`, what was done) for a script.

It removes three kinds of things.

**Idle throwaway platforms** in `~/.osy/instances`. A platform is a throwaway when it is a test platform (the one
`osy test` runs on), its project or CLI build is gone, it records no project, its project is a temporary directory, or
it was started with `OSY_INSTANCE_DISPOSABLE=1`. It is removed when it has served no request for `--idle-hours`
(default **2**). The platform's own scheduled work and health checks are not requests. A running one is stopped
first, the way `osy stop` stops one: it is asked to shut down and its database stops with it. It is never stopped
when:

- another process is connected to it, such as a test run, a UI test's driver, or a page in the middle of a request;
- another process runs from its CLI build, such as an `osy test` started from that build;
- something keeps it up: an open page of a platform launched for use, an attached debugger, or a program an app
  started;
- it is still starting, or it cannot be proven whether a server is running on it.

A platform you launched to use (`osy launch`, `osy studio`) and used in the last day is kept. So is the platform of
a project that still exists and is not a throwaway: only `osy instances --prune --yes`, which you read before you
confirm, deletes that one.

**Cached CLI builds** in `~/.osy/agent-cli` and the other CLI build caches under `~/.osy` (and `OSY_AGENT_CLI_CACHE`, when it names
another folder). A build is removed when all of these are true:

- it is not one of the newest `--keep-builds` (default **2**) in its folder;
- it is not being built;
- it is not held by a running caller (`.held-by-<pid>`);
- no platform that stays serves from it;
- no process runs from it, works in it, or has a file in it open.

A build whose platform this sweep stops is free once that platform is gone. `--apply` checks every build again after
the stops, so a build whose platform did not stop is kept.

**Publish caches** in `~/.osy/local-build`, which `osycloud local up` builds in. One goes when nothing in it changed
for `--cache-days` (default **2**) and no process uses it.

Before it acts, `--apply` checks everything again and acts only on what is still idle, because a test run can start
between the dry run and the action. Each item is deleted by its exact path, inside the folder it was found in.

`osy instances --prune` ([Reclaiming the disk your local platforms use](https://osysharp.com/reference/local/reclaiming-local-disk/)) is the other cleanup. It is for a person freeing disk:
it never stops a running platform, but with `--yes` it deletes any stopped one unused for its window, including the
platform of a project that still exists.

## Examples       {#examples}

```console
osy instances prune                      # what would go and why, and what stays — changes nothing
```

```console
osy instances prune --apply              # stop and remove it
```

```console
osy instances prune --apply --json       # the same, for a script: the plan and what was done
```

On a schedule, such as an hourly sweep, run it with its defaults: platforms idle 2 hours, builds beyond the newest 2
per folder, and publish caches unused 2 days.

```console
osy instances prune --apply --idle-hours 2 --cache-days 2 --keep-builds 2
```

## See also       {#see-also}

[Reclaiming the disk your local platforms use](https://osysharp.com/reference/local/reclaiming-local-disk/) — `osy instances --prune`, the cleanup you read before you confirm.

[Listing your local platforms](https://osysharp.com/reference/local/listing-local-instances/) — what is on this machine, which are running, and how much disk it takes.

[Stopping the local platform](https://osysharp.com/reference/local/stopping-the-local-platform/) — stop one platform by hand.
