Skip to content

Finding every CMake build folder on a Mac, and the one you must not delete

C and C++ debug builds leave large CMake build folders and a ccache store behind. How to find them all on a Mac, and how to tell a build folder from your source.

4 min read

Debug builds of C and C++ carry full debug information in every object file, so a build folder is often several times the size of the source it came from. CLion creates one per configuration (cmake-build-debug, cmake-build-release), and hand-made build folders multiply the same way.

Find every build folder

Every CMake build folder has a CMakeCache.txt at its top, so searching for that file finds them all, whatever they are called:

find ~ -maxdepth 6 -name CMakeCache.txt -not -path '*/.*' 2>/dev/null \
  | while read -r f; do du -sh "$(dirname "$f")"; done | sort -h

The one you must not delete

If a CMakeCache.txt sits next to your CMakeLists.txt, that project was built in-source, and the folder it lists is your source code. Do not delete it. Clean it from inside instead:

cmake --build . --target clean
rm -rf CMakeFiles CMakeCache.txt

For a normal out-of-source folder such as build or cmake-build-debug, deleting the whole folder is safe, and cmake -B build recreates it.

ccache

ccache -s          # size and location
ccache -M 5G       # cap it
ccache -C          # clear it

Related: vcpkg build trees, Xcode DerivedData and a safe developer cleanup script.

Common questions

Is it safe to delete cmake-build-debug?

Yes. It is an out-of-source build folder created by CLion. Deleting it removes compiled output only, and the IDE recreates it on the next build or reload.

How do I find all CMake build folders on my Mac?

Search for CMakeCache.txt, which CMake writes at the top of every build folder. Its parent folder is the build folder, and du -sh on each shows the sizes.

Where is ccache's cache on a Mac?

ccache -s prints the location and size. ccache -M sets a maximum size and ccache -C clears it, and compiles are simply slower until it fills again.

Read next