Skip to main content

Overview

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

Command Syntax

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:

Examples

Basic Clean

Output:

Before and After

Before clean:
After clean:

Use Cases

Fresh Build

Start with a clean slate:

Resolve Build Issues

Clear stale build artifacts:

Free Disk Space

Remove accumulated build artifacts:

Before Version Control

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

Comparison with Other Clean Commands

anchor clean vs cargo clean

Complete Clean

For a complete clean including all Cargo artifacts:

Automation

Clean Before Build Script

Add to package.json:
Usage:

CI/CD Pipeline

Preserving Keypairs

Program keypairs are critical and are never removed:
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:

Disk Space Impact

Typical space saved by anchor clean:

Rebuild After Clean

After cleaning, rebuild your workspace:

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