Skip to main content

Overview

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

Command Syntax

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

anchor clean
Output:
Cleaning target directory...
Done

Before and After

Before clean:
ls -la target/deploy/
total 1234
my_program.so
my_program-keypair.json
other_program.so
other_program-keypair.json
After clean:
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:
anchor clean
anchor build

Resolve Build Issues

Clear stale build artifacts:
anchor clean
anchor build --verifiable

Free Disk Space

Remove accumulated build artifacts:
# 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):
anchor clean
git status

Comparison with Other Clean Commands

anchor clean vs cargo clean

CommandScopeKeypairsSpeed
anchor cleanAnchor artifacts onlyPreservedFast
cargo cleanAll Rust build artifactsPreservedSlower
# 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:
anchor clean
cargo clean

Automation

Clean Before Build Script

Add to package.json:
{
  "scripts": {
    "clean": "anchor clean",
    "build": "anchor clean && anchor build",
    "rebuild": "anchor clean && cargo clean && anchor build"
  }
}
Usage:
npm run build

CI/CD Pipeline

# .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:
# 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:
# 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:
# 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:
# Standard build
anchor clean
anchor build

# Build and test
anchor clean
anchor test

# Verifiable build
anchor clean
anchor build --verifiable

Notes

While anchor clean preserves keypairs, always backup your target/deploy/*-keypair.json files separately, especially for production programs.
The .anchor directory contains Anchor’s internal cache. Removing it is safe and will be regenerated on the next build.
Run anchor clean if you encounter strange build errors or outdated IDL issues.
If you need to regenerate program keypairs (creating new program IDs), manually delete the keypair files after running anchor clean.

See Also

Build docs developers (and LLMs) love