# Clean up generated code

Generated files can contain imports a particular output does not use, for example when a pack emits a shared header and a project needs only part of it. Dryv does not rewrite those imports itself. It runs **actions** you choose on the staged files, before they are written, so the cleanup is explicit, repeatable and part of your project.

## One command

```bash
dryv action list                                  # the available presets
dryv action add clean-imports-typescript --yes    # TypeScript and JavaScript
dryv action add clean-imports-python --yes        # Python
```

This adds the action to `dryv.yaml` under `actions` and puts it **first** in the action list of every code destination, so it runs before your formatter. Use `--destination <key>` (repeatable) to attach it to specific destinations only, and `--dry-run` to preview. Running it again changes nothing.

| Preset                     | Runs                                                                | Needs                           |
| -------------------------- | ------------------------------------------------------------------- | ------------------------------- |
| `clean-imports-typescript` | Biome's `noUnusedImports` fix on `.ts .tsx .mts .cts .js .jsx .mjs` | Node.js (`npx` downloads Biome) |
| `clean-imports-python`     | Ruff's `F401` fix on `.py`                                          | `uv` (`uvx` runs Ruff)          |
| `clean-imports-go`         | `goimports -w` on `.go` (removes unused and adds missing imports)   | `goimports` on the `PATH`       |
| `clean-imports-dart`       | `dart fix --apply --code=unused_import` after files are written     | the Dart SDK                    |

The TypeScript and Python presets were run against real files and removed unused imports. The Go and Dart presets use those tools' standard commands but have not been run in CI.

## What it adds

```yaml
actions:
  clean-imports-typescript:
    stage: files
    extensions: [.ts, .tsx, .mts, .cts, .js, .jsx, .mjs]
    commands:
      - run:
          [
            npx,
            --yes,
            "@biomejs/biome@2",
            lint,
            --write,
            --unsafe,
            --only=correctness/noUnusedImports,
            $(files),
          ]

destinations:
  entities:
    path: src/entities
    actions:
      - { $ref: "#/actions/clean-imports-typescript" } # first: clean up
      - { $ref: "#/actions/format" } # then format
```

`stage: files` actions run on the staged output before Dryv compares and writes it, so the diff you review is already clean. `stage: final` actions run after files are written, from the destination directory.

> [!NOTE]
> Biome only removes unused imports when the rule is selected and `--unsafe` is passed. A plain `biome lint --write` does **not** remove them. Removing one name from `{ a, b }` can leave `{ a, }`, so keep a formatter action after it.

## Write your own

Any command that rewrites files works: `eslint --fix` with the `unused-imports` plugin, `ruff check --select F401,I --fix`, `gofmt`, `cargo fix`, `google-java-format --replace`. Declare it once under `actions` with a `stage`, and select it on destinations with `$ref`. See [dryv.yaml](/docs/usage/dryv-yaml) and the action rules in [Actions and commands](/pack-builders/design/actions).

## Tips

- Run `dryv generate --dry-run` first: the diff shows the cleaned files.
- `dryv generate --no-actions` skips every action, which is useful when debugging a template.
- Actions run argv directly, without a shell, and project-controlled commands need workspace trust (`dryv trust`).
- In CI, `dryv generate --check` fails when the output, cleaned by your actions, differs from what is committed.
