# How to Use `forc clean` to Clear Build Artifacts in Sway Projects

> Learn how to use forc clean to clear build artifacts in Sway projects. Ensure fresh builds by removing compiler generated bytecode and JSON ABIs using this essential command.

- Repository: [Fuel Labs/sway](https://github.com/FuelLabs/sway)
- Tags: how-to-guide
- Published: 2026-03-04

---

**`forc clean` deletes the `out/` directory containing compiler-generated artifacts for a Sway package or workspace, ensuring fresh builds by removing all generated bytecode and JSON ABIs.**

The `forc clean` command is the standard way to remove build artifacts in Sway projects maintained by FuelLabs. Whether you are debugging compilation issues or preparing a clean environment for CI/CD pipelines, understanding how to use `forc clean` effectively ensures you can manage the compiler's output directory with precision.

## What `forc clean` Deletes

`forc clean` removes the **output directory** (default: `<project>/out/`) that contains compiled bytecode, JSON ABIs, and other artifacts generated by the Sway compiler. The command operates at the manifest level, meaning it automatically detects whether you are working with a single Sway package or a **workspace** containing multiple members. If the output directory does not exist, the command completes silently without raising an error.

## Command Structure and CLI Options

The command-line interface for `forc clean` is defined in **[`forc/src/cli/commands/clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/cli/commands/clean.rs)**. The implementation uses `clap::Parser` to derive the command structure from a Rust struct.

The `Command` struct accepts one optional argument:

- `--path <PATH>`: Specifies the directory of the Sway project to clean. If omitted, the command uses the current working directory.

The `exec` function in [`forc/src/cli/commands/clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/cli/commands/clean.rs) serves as the thin wrapper that forwards the parsed command to the core operation:

```rust
pub fn exec(command: Command) -> ForcResult<()> {
    forc_clean::clean(command)?;
    Ok(())
}

```

## How `forc clean` Works Under the Hood

The core implementation resides in **[`forc/src/ops/forc_clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/ops/forc_clean.rs)**. The logic follows a systematic approach to locate the project manifest, determine the scope of cleaning, and safely remove artifacts.

### Manifest Resolution

First, the `clean` function determines the starting directory (`this_dir`) from the optional `--path` argument or the current working directory. It then invokes `sway_utils::find_parent_manifest_dir` to walk up the filesystem hierarchy and locate the nearest [`Forc.toml`](https://github.com/FuelLabs/sway/blob/main/Forc.toml). This ensures the command works correctly even when executed from a subdirectory within a package.

### Workspace Handling

Once the manifest is located, the code checks if it defines a workspace. If so, it retrieves all member paths using `workspace.member_paths()` and constructs a vector of directories to clean. For a single package, this vector contains only the `this_dir` value.

### Artifact Removal

For each member path, the command calculates the default output directory via `forc_util::default_output_directory(&member_path)`. It then removes the directory and all its contents using `std::fs::remove_dir_all(out_dir)`, silently ignoring errors when the directory does not exist:

```rust
let out_dir = default_output_directory(&member_path);
let _ = std::fs::remove_dir_all(out_dir);

```

The function returns `Ok(())` on success, while manifest-related errors propagate up as `anyhow::Result` for display to the user.

## Practical Usage Examples

### Clean the Current Project

Run the command from the root of any Sway package to remove the local `out/` directory:

```bash
forc clean

```

### Clean a Specific Directory

Use the `--path` flag to target a project located elsewhere without changing your current working directory:

```bash
forc clean --path ./examples/hello_world

```

### Clean an Entire Workspace

When executed from any folder inside a workspace, `forc clean` automatically locates the workspace root and deletes the `out/` folder of **every** member:

```bash

# Run from any subdirectory of the workspace

forc clean

```

### Programmatic Usage in Rust

To invoke the clean logic from within Rust code, import the command structure and execution function:

```rust
use forc::cli::commands::clean::Command;
use forc::cli::commands::clean::exec;

let cmd = Command { path: Some("path/to/project".into()) };
exec(cmd).expect("Failed to clean build artifacts");

```

## Key Source Files

The implementation of `forc clean` spans several files in the FuelLabs/sway repository:

- **[`forc/src/cli/commands/clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/cli/commands/clean.rs)** – Defines the CLI interface using `clap::Parser`, including the optional `--path` argument. Contains the `exec` entry point that delegates to the core operation.

- **[`forc/src/ops/forc_clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/ops/forc_clean.rs)** – Implements the core cleaning logic: resolves manifests using `sway_utils::find_parent_manifest_dir`, handles workspace members via `workspace.member_paths()`, and deletes `out/` directories using `std::fs::remove_dir_all`.

- **[`sway-utils/src/lib.rs`](https://github.com/FuelLabs/sway/blob/main/sway-utils/src/lib.rs)** – Provides the `find_parent_manifest_dir` utility function used to locate [`Forc.toml`](https://github.com/FuelLabs/sway/blob/main/Forc.toml) files by traversing parent directories.

- **[`forc-util/src/lib.rs`](https://github.com/FuelLabs/sway/blob/main/forc-util/src/lib.rs)** – Contains `default_output_directory`, which computes the standard `out/` path for a given project directory according to the compiler's conventions.

## Summary

- **`forc clean`** removes the `out/` directory containing compiler-generated artifacts for Sway packages and workspaces.
- The command is defined in [`forc/src/cli/commands/clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/cli/commands/clean.rs) and implemented in [`forc/src/ops/forc_clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/ops/forc_clean.rs).
- It automatically detects workspaces and cleans all member projects using `workspace.member_paths()`.
- Use the `--path` flag to target specific directories without changing your working directory.
- The operation is idempotent: it succeeds silently if the `out/` directory does not exist.

## Frequently Asked Questions

### What exactly does `forc clean` delete?

`forc clean` deletes the `out/` directory located at the root of your Sway project or workspace members. This directory contains compiled bytecode, JSON ABIs, and other artifacts generated by the Sway compiler during the build process. It does not delete your source code, [`Forc.toml`](https://github.com/FuelLabs/sway/blob/main/Forc.toml) manifest, or any other project files.

### Does `forc clean` work with Sway workspaces?

Yes. When executed from any directory within a workspace, `forc clean` automatically locates the workspace root via `sway_utils::find_parent_manifest_dir`, enumerates all members using `workspace.member_paths()`, and removes the `out/` directory for every member project. This ensures a complete clean build environment across the entire workspace.

### Is it safe to run `forc clean` multiple times?

Yes, `forc clean` is safe to run repeatedly. The implementation in [`forc/src/ops/forc_clean.rs`](https://github.com/FuelLabs/sway/blob/main/forc/src/ops/forc_clean.rs) uses `std::fs::remove_dir_all` and explicitly ignores errors when the target directory does not exist. This makes the command idempotent—you can run it before every build in CI/CD pipelines without worrying about failure states.

### How is `forc clean` different from manually deleting the `out/` folder?

While manually deleting the `out/` directory achieves the same result for a single package, `forc clean` provides additional safety and convenience. It automatically discovers the correct project root by searching for [`Forc.toml`](https://github.com/FuelLabs/sway/blob/main/Forc.toml), handles workspace member projects collectively, and uses the canonical output directory path computed by `forc_util::default_output_directory`. This prevents accidental deletion of wrong directories and ensures consistency with the compiler's internal logic.