# Cleaning the install directory

**URL:** <https://discourse.cmake.org/t/cleaning-the-install-directory/14318>\
**Category:** Code\
**Created:** [July 5, 2025, 8:32am UTC](https://discourse.cmake.org/t/cleaning-the-install-directory/14318 "2025-07-05T08:32:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)\
**Post date:** [July 5, 2025, 8:32am UTC](https://discourse.cmake.org/t/cleaning-the-install-directory/14318/1 "2025-07-05T08:32:28Z")

</div>

What’s the best way to ensure that the install directory is empty prior to building my project.

Ideally I’d like to put this in my top level CMake file, before I do the add\_subirectory stuff

The actual command to empty the directory is easy:

```auto
COMMAND ${CMAKE_COMMAND} -E rm -rf ${CMAKE_INSTALL_PREFIX}/*

```

The tricky bit is how to get that issued at the correct stage.

Thanks  
David

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [July 5, 2025, 11:57am UTC](https://discourse.cmake.org/t/cleaning-the-install-directory/14318/2 "2025-07-05T11:57:03Z")

</div>

### That is a really bad idea!

The default for `CMAKE_INSTALL_PREFIX` is `/usr/local`, and there is normally more than your project installed.

If you want a clean installation use `cpack`, it creates an archive with only your installation files.

---

<div class="post-metadata">

**Author:** ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)\
**Post date:** [July 5, 2025, 3:21pm UTC](https://discourse.cmake.org/t/cleaning-the-install-directory/14318/3 "2025-07-05T15:21:22Z")

</div>

That rather depends on what you have set CMAKE\_INSTALL\_PREFIX to

My builds install to a folder that is only there to hold the build output with all RPATHs adjusted to pick up the .so/.dylibs relative to the executables, and all related other files in an appropriate directory structure.

I then use that directory to create my installers.

So in my case it is **not** a really bad idea!

David

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [July 7, 2025, 10:05am UTC](https://discourse.cmake.org/t/cleaning-the-install-directory/14318/4 "2025-07-07T10:05:54Z")

</div>

You mean until someone makes a mistake?
