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

# anchor clean

> Remove all build artifacts except program keypairs

## Overview

The `anchor clean` command removes all generated build artifacts from the workspace while preserving program keypairs.

## Command Syntax

```bash theme={null}
anchor clean
```

## Description

This command cleans the workspace by removing:

* Compiled program binaries (`.so` files)
* IDL files (JSON and TypeScript)
* Build caches and intermediate files
* Test ledger data
* Docker build artifacts

**Preserved files:**

* Program keypairs (`*-keypair.json`)
* Source code
* Configuration files

## What Gets Removed

The following directories and files are cleaned:

```
target/
├── deploy/*.so           # ✓ Removed (program binaries)
├── deploy/*-keypair.json # ✗ Preserved (program keypairs)
├── idl/*.json            # ✓ Removed (IDL files)
├── types/*.ts            # ✓ Removed (TypeScript types)
├── verifiable/           # ✓ Removed (verifiable builds)
└── debug/                # ✓ Removed (debug builds)

.anchor/                  # ✓ Removed (Anchor cache)
test-ledger/              # ✓ Removed (test validator data)
.surfpool/                # ✓ Removed (Surfpool validator data)
```

## Examples

### Basic Clean

```bash theme={null}
anchor clean
```

**Output:**

```
Cleaning target directory...
Done
```

### Before and After

**Before clean:**

```bash theme={null}
ls -la target/deploy/
```

```
total 1234
my_program.so
my_program-keypair.json
other_program.so
other_program-keypair.json
```

**After clean:**

```bash theme={null}
anchor clean
ls -la target/deploy/
```

```
total 16
my_program-keypair.json
other_program-keypair.json
```

## Use Cases

### Fresh Build

Start with a clean slate:

```bash theme={null}
anchor clean
anchor build
```

### Resolve Build Issues

Clear stale build artifacts:

```bash theme={null}
anchor clean
anchor build --verifiable
```

### Free Disk Space

Remove accumulated build artifacts:

```bash theme={null}
# Check size before
du -sh target/

# Clean
anchor clean

# Check size after
du -sh target/
```

### Before Version Control

Clean before committing (though `target/` should be in `.gitignore`):

```bash theme={null}
anchor clean
git status
```

## Comparison with Other Clean Commands

### anchor clean vs cargo clean

| Command        | Scope                    | Keypairs  | Speed  |
| -------------- | ------------------------ | --------- | ------ |
| `anchor clean` | Anchor artifacts only    | Preserved | Fast   |
| `cargo clean`  | All Rust build artifacts | Preserved | Slower |

```bash theme={null}
# Anchor clean - removes deploy binaries, IDLs, types
anchor clean

# Cargo clean - removes all Cargo build artifacts
cargo clean
```

### Complete Clean

For a complete clean including all Cargo artifacts:

```bash theme={null}
anchor clean
cargo clean
```

## Automation

### Clean Before Build Script

Add to `package.json`:

```json theme={null}
{
  "scripts": {
    "clean": "anchor clean",
    "build": "anchor clean && anchor build",
    "rebuild": "anchor clean && cargo clean && anchor build"
  }
}
```

Usage:

```bash theme={null}
npm run build
```

### CI/CD Pipeline

```yaml theme={null}
# .github/workflows/test.yml
- name: Clean workspace
  run: anchor clean

- name: Build programs
  run: anchor build
```

## Preserving Keypairs

Program keypairs are critical and are never removed:

```bash theme={null}
# These are always preserved
target/deploy/my_program-keypair.json
target/deploy/other_program-keypair.json
```

Why keypairs are preserved:

* They define your program IDs
* Losing them means you can't upgrade deployed programs
* They're not easily regenerated (would create new program IDs)

## Manual Cleanup

If you need to remove specific artifacts:

```bash theme={null}
# Remove only IDL files
rm -rf target/idl/

# Remove only binaries
rm -f target/deploy/*.so

# Remove only test ledger
rm -rf test-ledger/

# Remove verifiable builds
rm -rf target/verifiable/
```

## Disk Space Impact

Typical space saved by `anchor clean`:

```bash theme={null}
# Example workspace
Before clean:
  target/       250 MB
  .anchor/       15 MB
  test-ledger/   50 MB
  Total:        315 MB

After clean:
  target/         2 MB  (keypairs only)
  .anchor/        -
  test-ledger/    -
  Total:          2 MB

Space saved:    313 MB
```

## Rebuild After Clean

After cleaning, rebuild your workspace:

```bash theme={null}
# Standard build
anchor clean
anchor build

# Build and test
anchor clean
anchor test

# Verifiable build
anchor clean
anchor build --verifiable
```

## Notes

<Warning>
  While `anchor clean` preserves keypairs, always backup your `target/deploy/*-keypair.json` files separately, especially for production programs.
</Warning>

<Info>
  The `.anchor` directory contains Anchor's internal cache. Removing it is safe and will be regenerated on the next build.
</Info>

<Tip>
  Run `anchor clean` if you encounter strange build errors or outdated IDL issues.
</Tip>

<Note>
  If you need to regenerate program keypairs (creating new program IDs), manually delete the keypair files after running `anchor clean`.
</Note>

## See Also

* [anchor build](/cli/build) - Build programs after cleaning
* [anchor keys list](/cli/keys) - Verify keypairs after cleaning
* [cargo clean](https://doc.rust-lang.org/cargo/commands/cargo-clean.html) - Cargo's clean command
