Repository: conan-io/conan

Issue:
[bug] Does 'conan cache clean' really clean everything in cache?
### Environment details

* Operating System+version: **_Windows 11 Enterprise_** (Version: `23H2`, OS build `22631.2428`)
* Compiler+version: -
* Conan version: `2.0.13`
* Python version: `3.11.2`

### Description

Hi,
According to [Conan 2 documentation](https://docs.conan.io/2/reference/commands/cache.html#conan-cache-clean ) (and see also [#14360](https://github.com/conan-io/conan/issues/14360)), I expected to get the Conan cache cleared (everything removed, dirty packages included) after execution of the following commands:
```
conan remove '*'
conan cache clean
conan cache clean -s -b -d -t
```
In fact, this is **not** what happens in my case and quite some stuff related to dirty packages is still there.
This unexpected behavior seems to be related to the following setting in my _global.conf_ file:
`core.cache:storage_path=C:\c2`

By removing this setting (i.e. by having the Conan storage folder in _C:\Users\myhome\\.conan2\p_) the same command sequence correctly removes everything in folders _C:\Users\myhome\\.conan2\p\t_ and _C:\Users\myhome\\.conan2\p\b_, but there are still not empty folders like _C:\Users\myhome\\.conan2\p\motio3f68cc067fec1\b_ and _C:\Users\myhome\\.conan2\p\motio3f68cc067fec1\p_ remaining.



Discussion:
Hi @marausch 

Thanks for reporting.
Quick question: When defining the ``core.cache:storage_path=C:\c2``, after the clean, what is left is empty folders only? Or are there some folders with files?

Can you reproduce with some specific flow? Like with a simple ``conan install --requires=zlib/1.2.13`` it will happen? Or maybe it happens when creating packages from source? Thanks for the feedback!
Hi @memsharded 

I cannot share the modules we are working on, but I think I managed to reproduce the issue with the following steps:

1. From an empty folder I started with:
```
conan new cmake_lib -d name=test_cache_clean -d version=0.1.0
```
2. Then I edited _conanfile.py_ adding the following line at the end of the _build()_ method:
`raise ConanException(f"Abort for debugging...")`
just to force an error on package building (no need to `import conan.errors`, BTW).
3. Finally, I tried to create the package with:
```
conan create -pr:b default -pr:h default --user demo --channel experimental .
```
At this point I tried to clear the conan cache with the three commands in question:
```
conan remove '*'
conan cache clean
conan cache clean -s -b -d -t
```
The following picture shows what I can see now in conan cache (I hope I've attached the picture correctly):
![image](https://github.com/conan-io/conan/assets/30287440/ad368b6a-9882-4bb3-a5bc-e38403f8e00d)

Note that I did this test after having deleted the conan cache folder (actually I just renamed the folder `c:\c2\` to `C:\c2sav`).
Originally the cache contained some additional folder such as `C:\c2\motio3f68cc067fec1\b\` containing subfolders and files that I wasn't able to clean away with the commands in question.
But I found out that this is old stuff I produced with previous versions of Conan and belonging to packages that weren't shown anymore by a `conan list '*'` command.

Same problem here. The clean command seems to do nothing. I tried to clean up everything with `conan cache clean "*"` but I still have a lot of folders in `$CACHE/p` and even in `$CACHE/p/b`. Testing in WSL2 with Conan 2.0.17.
But ``$CACHE/p`` is a perfectly valid folder that will contain downloaded packages, and also ``$CACHE/p/b`` is valid for packages that are built locally. This is expected, these are final packages that shouldn't be removed with ``conan cache clean``, these are not temporary folders.


But I assume these packages are used by Conan for certain tasks? I had to remove these folders manually to get my local build working again, because a package with a broken conanfile (which was previously uploaded to the remote) was used instead of the fixed version from the remote.
Maybe there is also a misunderstanding on my side about the term 'cache'. I assumed that `conan cache clean` would be equivalent to `rm -rf ~/.conan/data`.
> Maybe there is also a misunderstanding on my side about the term 'cache'. I assumed that conan cache clean would be equivalent to rm -rf ~/.conan/data.

No, not really. The ``conan cache clean`` is to clean temporary files to save space. It doesn't remove packages, that is done with the ``conan remove`` command.

```
$ conan cache clean -h
usage: conan cache clean [-h] [-v [V]] [-s] [-b] [-d] [-t] [-p PACKAGE_QUERY] [pattern]

Remove non-critical folders from the cache, like source, build and/or download (.tgz store) ones.
```
Hi,
I've repeated the test I made on Nov 13, 2023 after having updated to Conan 2.0.16.
I also noticed that, according to the [current documentation](https://docs.conan.io/2/reference/commands/cache.html#conan-cache-clean), I might have used a wrong syntax:
```
conan cache clean
```
instead of:
```
conan cache clean '*'
```
So, this time I did:
```
conan remove '*'
conan cache clean '*'
conan cache clean -s -b -d -t '*'
```
but the result didn't change. There are still the same files and folder left in the cache (apart from some obvious differences).


Thanks for the feedback @marausch 

I have been able to identify the issue, it was indeed specific of using ``core.cache:storage_path``
I am trying a fix in https://github.com/conan-io/conan/pull/15505, if you want to try it from source, feedback welcome.



Patch:
diff --git a/conans/client/cache/cache.py b/conans/client/cache/cache.py
index aa25c8199bd..8c494cf0252 100644
--- a/conans/client/cache/cache.py
+++ b/conans/client/cache/cache.py
@@ -35,11 +35,11 @@ def temp_folder(self):
         """ temporary folder where Conan puts exports and packages before the final revision
         is computed"""
         # TODO: Improve the path definitions, this is very hardcoded
-        return os.path.join(self.cache_folder, "p", "t")
+        return os.path.join(self._store_folder, "t")
 
     @property
     def builds_folder(self):
-        return os.path.join(self.cache_folder, "p", "b")
+        return os.path.join(self._store_folder, "b")
 
     def create_export_recipe_layout(self, ref: RecipeReference):
         return self._data_cache.create_export_recipe_layout(ref)


Test patch:
diff --git a/conans/test/integration/command_v2/test_cache_clean.py b/conans/test/integration/command_v2/test_cache_clean.py
index a9f39daf9aa..baace9551eb 100644
--- a/conans/test/integration/command_v2/test_cache_clean.py
+++ b/conans/test/integration/command_v2/test_cache_clean.py
@@ -1,7 +1,12 @@
 import os.path
+import re
+
+import pytest
 
 from conans.test.assets.genconanfile import GenConanfile
+from conans.test.utils.test_files import temp_folder
 from conans.test.utils.tools import TestClient
+from conans.util.files import save
 
 
 def test_cache_clean():
@@ -113,3 +118,16 @@ def test_cache_multiple_builds_diff_prev_clean():
     assert len(os.listdir(builds_folder)) == 2  # two builds will remain, both are valid
     c.run('remove * -c')
     assert len(os.listdir(builds_folder)) == 0  # no folder remain
+
+
+def test_cache_clean_custom_storage():
+    c = TestClient()
+    t = temp_folder(path_with_spaces=False)
+    save(c.cache.global_conf_path, f"core.cache:storage_path={t}")
+    c.save({"conanfile.py": GenConanfile("pkg", "0.1").with_cmake_build()})
+    c.run("create .", assert_error=True)
+    build_folder = re.search(r"pkg/0.1: Building your package in (\S+)", str(c.out)).group(1)
+    assert os.listdir(build_folder)
+    # now clean
+    c.run("cache clean")
+    assert not os.path.exists(build_folder)