Repository navigation
Description
👟 Reproduction steps
Run this script using the containerized PHP CLI:
docker compose exec appwrite php -r '
require_once "vendor/autoload.php";
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache;
$path = sys_get_temp_dir() . "/cache-bug-repro";
@mkdir($path, 0777, true);
$adapter = new Filesystem($path);
$cache = new Cache($adapter);
// 1. Flushing an empty directory throws an exception
try {
$cache->flush();
} catch (\Throwable $e) {
echo "Bug 1: " . $e->getMessage() . "\n";
}
// 2. Flushing a populated cache deletes the root folder
$cache->save("item", "value");
$cache->flush();
echo "Directory exists: " . (is_dir($path) ? "true" : "false") . "\n";
echo "Ping: " . ($cache->ping() ? "true" : "false") . "\n";
// 3. Subsequent flush crashes
try {
$cache->flush();
} catch (\Throwable $e) {
echo "Bug 3: " . $e->getMessage() . "\n";
}
'👍 Expected behavior
- Calling
flush()on an empty cache should succeed without throwing an exception and returntrue. - Calling
flush()on a populated cache should remove all cached files and subdirectories, but keep the root directory intact. $cache->ping()should remaintrueafterflush(), and subsequent calls toflush()should succeed idempotently.
👎 Actual Behavior
-
Crash when flushing an empty cache:
deleteDirectory()reads directory entries usingglob($path . '*', GLOB_MARK). On an empty cache directory,glob()returns[](empty array). The error checkif (! $files)evaluates totrue, mistakenly treating an empty directory as a glob failure and throwingException: Error happened during glob. -
Root cache directory is deleted:
deleteDirectory()finishes by executingrmdir($path). When called fromflush(),$pathis$this->path(the root cache folder). Rather than wiping only the cached files and subdirectories, it removes the entire root directory from disk. -
Subsequent
ping()and repeatedflush()fail:
Because the root directory has been deleted:$cache->ping()immediately returnsfalse(file_exists($this->path)is false).- Calling
$cache->flush()again crashes withException: <path> must be a directorybecause$this->pathno longer exists.
🎲 Appwrite version
Appwrite Cloud
💻 Operating system
Linux
🧱 Your Environment
No response
👀 Have you spent some time to check if this issue has been raised before?
- I checked and didn't find similar issue
🏢 Have you read the Code of Conduct?
- I have read the Code of Conduct
Activity
Spandan-Mishra commentedon Sep 30, 2026
I have investigated this issue and reproduced the root cause down to lines 165 and 177 in
packages/cache/src/Adapter/Filesystem.php.The fix requires:
if ($files === false)instead ofif (! $files)so empty directory returns ([]) do not trigger an exception.deleteDirectory()calls initiated byflush(), while still recursively removing emptied subdirectories and unlinking files.I'd be grateful if i can be assigned to this so that i can raise the PR for it.
piyushthawale19 commentedon Oct 2, 2026
Hi! I'd like to work on this issue. Could you please assign it to me?