Skip to content

/commands/cmake/clean-build-directory

cmake clean build: --target clean, --clean-first and --fresh

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
CMake
versions
CMake 3.24+ (--fresh), CMake 4.x
os
linux, macos, windows

To clean a CMake build, run cmake --build build --target clean: it deletes what the build produced and keeps the configuration. To clean and rebuild in one step, use cmake --build build --clean-first. To also throw away the cache (options, found compilers and libraries), reconfigure with cmake --fresh -S . -B build (CMake 3.24 and later) or delete the build directory.

Which command resets what

CommandRemovesKeeps
cmake --build build --target cleanbuilt files: objects, libraries, executables, generated outputsCMakeCache.txt and the generated build system
cmake --build build --clean-firstthe same, then builds againthe cache
cmake --fresh -S . -B buildCMakeCache.txt and CMakeFiles/, then configures againbuilt files until the next build overwrites them
cmake -E rm -rf buildthe whole build directorynothing; reconfigure from scratch

The first two commands go through the native build tool, so they behave like make clean or ninja -t clean for the generator you chose.

Clean and rebuild

The project below generates one file with a custom command, so it needs no compiler. CMake comes from PyPI through uv here; the commands are the same with any installed CMake:

bash
cat > CMakeLists.txt <<'EOF'
cmake_minimum_required(VERSION 3.20)
project(demo NONE)
add_custom_command(OUTPUT greeting.txt
  COMMAND ${CMAKE_COMMAND} -E echo_append hello > greeting.txt
  COMMENT "Generating greeting.txt")
add_custom_target(greet ALL DEPENDS greeting.txt)
EOF
cmake() { uv run -q --no-project --python 3.13 --with cmake==4.4.4 --with ninja==1.13.0 cmake "$@"; }
cmake -S . -B build -G Ninja > /dev/null
cmake --build build
echo '--- clean'
cmake --build build --target clean
ls build
echo '--- clean first'
cmake --build build --clean-first

Output

[1/1] Generating greeting.txt
--- clean
[1/1] Cleaning all built files...
Cleaning... 1 files.
CMakeCache.txt
CMakeFiles
build.ninja
cmake_install.cmake
--- clean first
[1/1] Cleaning all built files...
Cleaning... 0 files.
[1/1] Generating greeting.txt

After clean, greeting.txt is gone but CMakeCache.txt and build.ninja remain, so the next build does not reconfigure.

Reset the cache with --fresh

A value passed with -D stays in the cache until something removes it. --fresh drops the cache and configures as if the directory were new:

bash
cat > CMakeLists.txt <<'EOF'
cmake_minimum_required(VERSION 3.20)
project(demo NONE)
set(GREETING "hello" CACHE STRING "Greeting text")
EOF
cmake() { uv run -q --no-project --python 3.13 --with cmake==4.4.4 --with ninja==1.13.0 cmake "$@"; }
cmake -S . -B build -G Ninja -DGREETING=bye > /dev/null
cmake -S . -B build > /dev/null && cmake -L -N build | grep GREETING
cmake --fresh -S . -B build -G Ninja > /dev/null && cmake -L -N build | grep GREETING

Output

GREETING:STRING=bye
GREETING:STRING=hello

The second configure still reports bye because the cache wins over the default in CMakeLists.txt; only --fresh brings back hello.

Pitfalls

  • Pass the generator again with --fresh. Without the cache, CMake picks its default generator; on Windows with Visual Studio installed that is a Visual Studio generator, not Ninja.
  • Cleaning the source tree. An in-source build (cmake . in the source directory) mixes generated files with your sources; clean does not remove CMakeCache.txt or CMakeFiles/. Use a separate -B build directory, see cmake -S . -B build.
  • Changing compilers. CMake reads the compiler once and caches it. After installing a compiler, a stale cache can still fail with No CMAKE_C_COMPILER could be found; --fresh fixes it.
  • Deleting with rm. cmake -E rm -rf build works the same on Windows, Linux and macOS, which is useful in scripts; double-check the path first.

FAQ

Does cmake --build build --target clean delete CMakeCache.txt?

No. The clean target removes the files the build produced, but the cache and the generated build system stay, so the next build runs with the same options. Use cmake --fresh or delete the build directory to reset the cache.

Is there a cmake clean command like make clean?

cmake --build DIR --target clean is the portable form: it runs make clean, ninja clean or the Visual Studio clean target, whichever generator the directory uses.

When do I need to delete the whole build directory?

When you change the generator or the compiler, or when the cache holds a stale value you cannot find. --fresh covers most of these cases since CMake 3.24; deleting the directory is the last resort and always works.

Sources