Overview
Theanchor 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 (
.sofiles) - IDL files (JSON and TypeScript)
- Build caches and intermediate files
- Test ledger data
- Docker build artifacts
- Program keypairs (
*-keypair.json) - Source code
- Configuration files
What Gets Removed
The following directories and files are cleaned:Examples
Basic Clean
Before and After
Before 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 (thoughtarget/ should be in .gitignore):
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 |
Complete Clean
For a complete clean including all Cargo artifacts:Automation
Clean Before Build Script
Add topackage.json:
CI/CD Pipeline
Preserving Keypairs
Program keypairs are critical and are never removed:- 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 byanchor clean:
Rebuild After Clean
After cleaning, rebuild your workspace:Notes
The
.anchor directory contains Anchor’s internal cache. Removing it is safe and will be regenerated on the next build.If you need to regenerate program keypairs (creating new program IDs), manually delete the keypair files after running
anchor clean.See Also
- anchor build - Build programs after cleaning
- anchor keys list - Verify keypairs after cleaning
- cargo clean - Cargo’s clean command