> ## Documentation Index
> Fetch the complete documentation index at: https://glotctl.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# glot clean

> Remove unused translation keys from locale files

The `clean` command removes unused or orphan translation keys from your locale JSON files.

## Usage

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean [OPTIONS]
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean [OPTIONS]
  ```

  ```bash yarn theme={null}
  yarn glot clean [OPTIONS]
  ```

  ```bash bun theme={null}
  bunx glot clean [OPTIONS]
  ```
</CodeGroup>

## Options

<ParamField path="--apply" type="boolean" default="false">
  Actually delete keys. Without this flag, glot runs in dry-run mode and only
  shows what would be deleted.
</ParamField>

<ParamField path="--rules" type="string[]" default="all">
  Rules to clean. Can be specified multiple times. If not specified, all rules are applied.

  Possible values:

  * `unused` - Keys defined in locale files but not used in code
  * `orphan` - Keys in non-primary locales but not in primary locale
</ParamField>

<ParamField path="--source-root" type="string">
  Override the source code root directory. Defaults to the value from configuration file.
</ParamField>

<ParamField path="--messages-root" type="string">
  Override the messages directory path. Defaults to the value from configuration file.
</ParamField>

<ParamField path="--primary-locale" type="string">
  Override the primary locale. Defaults to the value from configuration file.
</ParamField>

<ParamField path="-v, --verbose" type="boolean" default="false">
  Enable verbose output for debugging.
</ParamField>

## Dry-Run Mode (Default)

By default, glot runs in dry-run mode to preview changes:

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean
  ```

  ```bash yarn theme={null}
  yarn glot clean
  ```

  ```bash bun theme={null}
  bunx glot clean
  ```
</CodeGroup>

Output:

```
Dry-run mode: showing keys that would be deleted

Would delete from messages/en.json:
  - common.oldButton
  - settings.deprecated

Would delete from messages/es.json:
  - common.oldButton
  - settings.deprecated
  - extra.orphanKey (orphan - not in primary locale)

4 keys would be deleted. Run with --apply to delete.
```

## Apply Changes

To actually delete the keys:

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean --apply
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean --apply
  ```

  ```bash yarn theme={null}
  yarn glot clean --apply
  ```

  ```bash bun theme={null}
  bunx glot clean --apply
  ```
</CodeGroup>

Output:

```
Deleted from messages/en.json:
  - common.oldButton
  - settings.deprecated

Deleted from messages/es.json:
  - common.oldButton
  - settings.deprecated
  - extra.orphanKey

✓ 4 keys deleted
```

## Clean Specific Key Types

### Unused Keys Only

Remove keys that exist in locale files but are never used in code:

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean --rules unused --apply
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean --rules unused --apply
  ```

  ```bash yarn theme={null}
  yarn glot clean --rules unused --apply
  ```

  ```bash bun theme={null}
  bunx glot clean --rules unused --apply
  ```
</CodeGroup>

### Orphan Keys Only

Remove keys that exist in non-primary locales but not in the primary locale:

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean --rules orphan --apply
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean --rules orphan --apply
  ```

  ```bash yarn theme={null}
  yarn glot clean --rules orphan --apply
  ```

  ```bash bun theme={null}
  bunx glot clean --rules orphan --apply
  ```
</CodeGroup>

<Note>
  When `--rules` is not specified, both `unused` and `orphan` keys are cleaned.
</Note>

## Safety Checks

Glot includes safety mechanisms to prevent accidental data loss:

<Warning>
  Glot will refuse to clean if:

  * **Dynamic keys are detected** - Template literals with expressions like ``t(`key.${id}`)`` can't be statically analyzed
  * **Parse errors exist** - Some files couldn't be parsed correctly
</Warning>

If you see this message:

```
Cannot clean: dynamic keys detected in codebase
These keys use runtime values and cannot be statically analyzed:
  - src/components/Dynamic.tsx:15 - t(`items.${item.type}`)

Fix or suppress these before cleaning.
```

You'll need to either:

1. Replace dynamic keys with static alternatives
2. Use `// glot-message-keys "pattern.*"` to declare which keys are used dynamically

<Warning>
  Do NOT use `glot-disable-next-line` for dynamic keys when using `clean`. While
  it suppresses the warning, it doesn't tell glot which keys are actually used.
  This means `clean` may incorrectly delete keys that are dynamically
  referenced.
</Warning>

### Handling Dynamic Keys

```tsx theme={null}
// Before: Dynamic key causes clean to fail
{
  items.map((item) => t(`items.${item.type}`));
}

// After: Declare expected keys with explicit list
// glot-message-keys "items.basic", "items.premium", "items.enterprise"
{
  items.map((item) => t(`items.${item.type}`));
}

// Or with glob pattern to match all keys under items.*
// glot-message-keys "items.*"
{
  items.map((item) => t(`items.${item.type}`));
}
```

## Examples

### Preview All Cleanable Keys

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean
  ```

  ```bash yarn theme={null}
  yarn glot clean
  ```

  ```bash bun theme={null}
  bunx glot clean
  ```
</CodeGroup>

### Clean Everything

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean --apply
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean --apply
  ```

  ```bash yarn theme={null}
  yarn glot clean --apply
  ```

  ```bash bun theme={null}
  bunx glot clean --apply
  ```
</CodeGroup>

### Clean Only Unused Keys with Custom Source Root

<CodeGroup>
  ```bash npm theme={null}
  npx glot clean --rules unused --apply --source-root ./src
  ```

  ```bash pnpm theme={null}
  pnpm exec glot clean --rules unused --apply --source-root ./src
  ```

  ```bash yarn theme={null}
  yarn glot clean --rules unused --apply --source-root ./src
  ```

  ```bash bun theme={null}
  bunx glot clean --rules unused --apply --source-root ./src
  ```
</CodeGroup>

## Related

<CardGroup cols={2}>
  <Card title="Directives" icon="code" href="/directives">
    Use glot-message-keys for dynamic keys
  </Card>

  <Card title="Orphan Keys" icon="broom" href="/detection/orphan-keys">
    Understanding orphan key detection
  </Card>

  <Card title="Missing Keys" icon="triangle-exclamation" href="/detection/missing-keys">
    Understanding missing key detection
  </Card>
</CardGroup>
