# Custom rules (/docs/guides/custom-rules)

Rules are **data**, not code: a config can always be validated, printed with [`purgeit rules`](/docs/reference/cli#rules), and shared. Put them in `purgeit.config.ts` (or `.json`, `.mjs`, …, or the `purgeit` key of `package.json`). purgeit uses the **nearest config, searching from the current directory upward**, then a user-level `config.*` in `~/.config/purgeit/`; the nearest one wins outright. `purgeit init` writes a starter. See the full [configuration reference](/docs/reference/configuration).

```ts title="purgeit.config.ts"
import { defineConfig } from 'purgeit';

export default defineConfig({
  /* … */
});
```

## Recipes [#recipes]

### A cache that is always safe to delete [#a-cache-that-is-always-safe-to-delete]

```ts
safe: ['.my-tool-cache'],
```

### A generic name, but only next to proof [#a-generic-name-but-only-next-to-proof]

Use a *gated* rule when the name alone is too generic. `out` is only generated output when a `project.toml` sits beside it:

```ts
gated: [{ name: 'out', when: { file: 'project.toml' } }],
```

`when` accepts one condition or a list of which any one suffices:

```ts
{ file: 'package.json' }                              // a sibling file
{ glob: '*.csproj' }                                  // a sibling matching a glob
{ grep: { file: 'Makefile', pattern: '^build:' } }    // a sibling whose contents match
{ inside: 'pyvenv.cfg' }                              // a file inside the directory itself
```

`inside` is how a bare name like `venv` is proved: the built-in rule only matches a `venv` that contains a `pyvenv.cfg`.

### Any directory carrying a tag file [#any-directory-carrying-a-tag-file]

```ts
markers: [{ name: 'MYTOOL.TAG', file: '.mytool-cache', signature: 'mytool-cache-v1' }],
```

### Keep things out [#keep-things-out]

```ts
ignore: ['vault', 'secrets'],   // never entered, anywhere
exclude: ['legacy/**'],         // skipped, relative to each scanned root
```

### Switch off a built-in rule [#switch-off-a-built-in-rule]

```ts
disable: ['coverage'],          // unknown names are an error, so typos are caught
```

### Named groups for `--only` [#named-groups-for---only]

```ts
groups: { web: ['node_modules', '.next', 'dist'] },
// purgeit clean --only web
```

### Defaults for flags [#defaults-for-flags]

```ts
minAge: '14d', minSize: '50MB', depth: 6,
dormantAfter: '14d',   // when a project counts as dormant (default 30d)
```

Flags always override config.

## JSON config with editor completion [#json-config-with-editor-completion]

```json
{ "$schema": "./node_modules/purgeit/schema.json", "safe": [".my-tool-cache"] }
```

## Things worth knowing [#things-worth-knowing]

- A rule with the same name as a built-in **replaces** it.
- A name can't be both `safe` and `gated` in one config — that is rejected.
- Version-control directories are never entered, whatever the config says.
- Matching a name is never enough on its own: [protection checks](/docs/concepts/safety-model) still apply to every match.