/commands/cmake/clean-build-directory
cmake clean build: --target clean, --clean-first and --fresh
- 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
| Command | Removes | Keeps |
|---|---|---|
| cmake --build build --target clean | built files: objects, libraries, executables, generated outputs | CMakeCache.txt and the generated build system |
| cmake --build build --clean-first | the same, then builds again | the cache |
| cmake --fresh -S . -B build | CMakeCache.txt and CMakeFiles/, then configures again | built files until the next build overwrites them |
| cmake -E rm -rf build | the whole build directory | nothing; 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:
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-firstOutput
[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:
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 GREETINGOutput
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;cleandoes not removeCMakeCache.txtorCMakeFiles/. Use a separate-B builddirectory, 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;
--freshfixes it. - Deleting with rm.
cmake -E rm -rf buildworks 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.