custom - Looking for a 'cmake clean' command to clear up CMake output

target cache (16)

In the case where you pass -D parameters into CMake when generating the build files and don't want to delete the entire build/ directory:

Simply delete the CMakeFiles/ directory inside your build directory.

rm -rf CMakeFiles/
cmake --build .

This causes CMake to rerun, and build system files are regenerated. Your build will also start from scratch.

Just as make clean deletes all the files that a makefile has produced, I would like to do the same with CMake. All too often I find myself manually going through directories removing files like cmake_install.cmake and CMakeCache.txt, and the CMakeFiles folders.

Is there a command like cmake clean to remove all these files automatically? Ideally this should follow the recursive structure defined within the current directory's CMakeLists.txt file.

To simplify cleaning when using "out of source" build (i.e. you build in the build directory), I use the following script:

$ cat ~/bin/cmake-clean-build

if [ -d ../build ]; then
    cd ..
    rm -rf build
    mkdir build
    cd build
    echo "build directory DOES NOT exist"

Every time you need to clean up, you should source this script from the build directory:

. cmake-clean-build

You can use something like:

   COMMAND ${CMAKE_COMMAND} -P clean-all.cmake

// clean-all.cmake
set(cmake_generated ${CMAKE_BINARY_DIR}/CMakeCache.txt

foreach(file ${cmake_generated})

  if (EXISTS ${file})
     file(REMOVE_RECURSE ${file})


I usually create a "make clean-all" command adding a call to "make clean" to the previous example:

   COMMAND ${CMAKE_COMMAND} -P clean-all.cmake

Don't try to add the "clean" target as a dependence:

   COMMAND ${CMAKE_COMMAND} -P clean-all.cmake
   DEPENDS clean

Because "clean" isn't a real target in CMake and this doesn't work.

Moreover, you should not use this "clean-cmake-files" as dependence of anything:

   DEPENDS clean-cmake-files

Because, if you do that, all CMake files will be erased before clean-all is complete, and make will throw you an error searching "CMakeFiles/clean-all.dir/build.make". In consequence, you can not use the clean-all command before "anything" in any context:

   COMMAND ${CMAKE_COMMAND} -P clean-all.cmake

That doesn't work either.

Simply issuing rm CMakeCache.txt works for me too.

I agree that the out-of-source build is the best answer. But for the times when you just must do an in-source build, I have written a Python script available here, which:

  1. Runs "make clean"
  2. Removes specific CMake-generated files in the top-level directory such as CMakeCache.txt
  3. For each subdirectory that contains a CMakeFiles directory, it removes CMakeFiles, Makefile, cmake_install.cmake.
  4. Removes all empty subdirectories.

I used zsxwing's answer successfully to solve the following problem:

I have source that I build on multiple hosts (on a Raspberry Pi Linux board, on a VMware Linux virtual machine, etc.)

I have a Bash script that creates temporary directories based on the hostname of the machine like this:

# Get hostname to use as part of directory names
HOST_NAME=`uname -n`

# Create a temporary directory for cmake files so they don't
# end up all mixed up with the source.


if [ ! -e $TMP_DIR ] ; then
  echo "Creating directory for cmake tmp files : $TMP_DIR"
  mkdir $TMP_DIR
  echo "Reusing cmake tmp dir : $TMP_DIR"

# Create makefiles with CMake
# Note: switch to the temporary dir and build parent 
#       which is a way of making cmake tmp files stay
#       out of the way.
# Note 2: to clean up cmake files, it is OK to
#        "rm -rf" the temporary directories

echo Creating Makefiles with cmake ...


cmake ..

# Run makefile (in temporary directory)

echo Starting build ...


I googled it for like half an hour and the only useful thing I came up with was invoking the find utility:

# Find and then delete all files under current directory (.) that:
#  1. contains "cmake" (case-&insensitive) in its path (wholename)
#  2. name is not CMakeLists.txt
find . -iwholename '*cmake*' -not -name CMakeLists.txt -delete

Also, be sure to invoke make clean (or whatever CMake generator you're using) before that.


If you run

cmake .

it will regenerate the CMake files. Which is necessary if you add a new file to a source folder that is selected by *.cc, for example.

While this isn't a "clean" per se, it does "clean" up the CMake files by regenerating the caches.

If you have custom defines and want to save them before cleaning, run the following in your build directory:

sed -ne '/variable specified on the command line/{n;s/.*/-D \0 \\/;p}' CMakeCache.txt

Then create a new build directory (or remove the old build directory and recreate it) and finally run cmake with the arguments you'll get with the script above.

I have this in my shell rc file (.bashrc, .zshrc):

t-cmake-clean() {
    local BUILD=$(basename $(pwd))
    cd ..
    rm -rf $BUILD
    mkdir $BUILD && cd $BUILD

You are supposed to use it only for out-of-source builds. Let's say you have a directory named build/ for this purpose. Then you just have to run t-cmake-clean from within it.

In these days of Git everywhere, you may forget CMake and use git clean -d -f -x, that will remove all files not under source control.

CMake official FAQ states:

Some build trees created with GNU autotools have a "make distclean" target that cleans the build and also removes Makefiles and other parts of the generated build system. CMake does not generate a "make distclean" target because CMakeLists.txt files can run scripts and arbitrary commands; CMake has no way of tracking exactly which files are generated as part of running CMake. Providing a distclean target would give users the false impression that it would work as expected. (CMake does generate a "make clean" target to remove files generated by the compiler and linker.)

A "make distclean" target is only necessary if the user performs an in-source build. CMake supports in-source builds, but we strongly encourage users to adopt the notion of an out-of-source build. Using a build tree that is separate from the source tree will prevent CMake from generating any files in the source tree. Because CMake does not change the source tree, there is no need for a distclean target. One can start a fresh build by deleting the build tree or creating a separate build tree.

A solution that I found recently is to combine the out-of-source build concept with a Makefile wrapper.

In my top-level CMakeLists.txt file, I include the following to prevent in-source builds:

    message( FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt." )

Then, I create a top-level Makefile, and include the following:

# -----------------------------------------------------------------------------
# CMake project wrapper Makefile ----------------------------------------------
# -----------------------------------------------------------------------------

SHELL := /bin/bash
RM    := rm -rf
MKDIR := mkdir -p

all: ./build/Makefile
    @ $(MAKE) -C build

    @  ($(MKDIR) build > /dev/null)
    @  (cd build > /dev/null 2>&1 && cmake ..)

    @  ($(MKDIR) build > /dev/null)
    @  (cd build > /dev/null 2>&1 && cmake .. > /dev/null 2>&1)
    @- $(MAKE) --silent -C build clean || true
    @- $(RM) ./build/Makefile
    @- $(RM) ./build/src
    @- $(RM) ./build/test
    @- $(RM) ./build/CMake*
    @- $(RM) ./build/cmake.*
    @- $(RM) ./build/*.cmake
    @- $(RM) ./build/*.txt

ifeq ($(findstring distclean,$(MAKECMDGOALS)),)
    $(MAKECMDGOALS): ./build/Makefile
    @ $(MAKE) -C build $(MAKECMDGOALS)

The default target all is called by typing make, and invokes the target ./build/Makefile.

The first thing the target ./build/Makefile does is to create the build directory using $(MKDIR), which is a variable for mkdir -p. The directory build is where we will perform our out-of-source build. We provide the argument -p to ensure that mkdir does not scream at us for trying to create a directory that may already exist.

The second thing the target ./build/Makefile does is to change directories to the build directory and invoke cmake.

Back to the all target, we invoke $(MAKE) -C build, where $(MAKE) is a Makefile variable automatically generated for make. make -C changes the directory before doing anything. Therefore, using $(MAKE) -C build is equivalent to doing cd build; make.

To summarize, calling this Makefile wrapper with make all or make is equivalent to doing:

mkdir build
cd build
cmake ..

The target distclean invokes cmake .., then make -C build clean, and finally, removes all contents from the build directory. I believe this is exactly what you requested in your question.

The last piece of the Makefile evaluates if the user-provided target is or is not distclean. If not, it will change directories to build before invoking it. This is very powerful because the user can type, for example, make clean, and the Makefile will transform that into an equivalent of cd build; make clean.

In conclusion, this Makefile wrapper, in combination with a mandatory out-of-source build CMake configuration, make it so that the user never has to interact with the command cmake. This solution also provides an elegant method to remove all CMake output files from the build directory.

P.S. In the Makefile, we use the prefix @ to suppress the output from a shell command, and the prefix @- to ignore errors from a shell command. When using rm as part of the distclean target, the command will return an error if the files do not exist (they may have been deleted already using the command line with rm -rf build, or they were never generated in the first place). This return error will force our Makefile to exit. We use the prefix @- to prevent that. It is acceptable if a file was removed already; we want our Makefile to keep going and remove the rest.

Another thing to note: This Makefile may not work if you use a variable number of CMake variables to build your project, for example, cmake .. -DSOMEBUILDSUSETHIS:STRING="foo" -DSOMEOTHERBUILDSUSETHISTOO:STRING="bar". This Makefile assumes you invoke CMake in a consistent way, either by typing cmake .. or by providing cmake a consistent number of arguments (that you can include in your Makefile).

Finally, credit where credit is due. This Makefile wrapper was adapted from the Makefile provided by the C++ Application Project Template.

Of course, out-of-source builds are the go-to method for Unix Makefiles, but if you're using another generator such as Eclipse CDT, it prefers you to build in-source. In which case, you'll need to purge the CMake files manually. Try this:

find . -name 'CMakeCache.txt' -o -name '*.cmake' -o -name 'Makefile' -o -name 'CMakeFiles' -exec rm -rf {} +

Or if you've enabled globstar with shopt -s globstar, try this less disgusting approach instead:

rm -rf **/CMakeCache.txt **/*.cmake **/Makefile **/CMakeFiles

There is no cmake clean.

I usually build the project in a single folder like "build". So if I want to make clean, I can just rm -rf build.

The "build" folder in the same directory as the root "CMakeLists.txt" is usually a good choice. To build your project, you simply give cmake the location of the CMakeLists.txt as an argument. For example: cd <location-of-cmakelists>/build && cmake ... (From @ComicSansMS)

Probably not. You might have to look at the source code of CMake.