Skip to content

Calling flush() on a cache backed by the Filesystem adapter leads runtime failures #14007

Closed

Description

@Spandan-Mishra

👟 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

  1. Calling flush() on an empty cache should succeed without throwing an exception and return true.
  2. Calling flush() on a populated cache should remove all cached files and subdirectories, but keep the root directory intact.
  3. $cache->ping() should remain true after flush(), and subsequent calls to flush() should succeed idempotently.

👎 Actual Behavior

  1. Crash when flushing an empty cache:
    deleteDirectory() reads directory entries using glob($path . '*', GLOB_MARK). On an empty cache directory, glob() returns [] (empty array). The error check if (! $files) evaluates to true, mistakenly treating an empty directory as a glob failure and throwing Exception: Error happened during glob.

  2. Root cache directory is deleted:
    deleteDirectory() finishes by executing rmdir($path). When called from flush(), $path is $this->path (the root cache folder). Rather than wiping only the cached files and subdirectories, it removes the entire root directory from disk.

  3. Subsequent ping() and repeated flush() fail:
    Because the root directory has been deleted:

    • $cache->ping() immediately returns false (file_exists($this->path) is false).
    • Calling $cache->flush() again crashes with Exception: <path> must be a directory because $this->path no 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?

Activity

  1. added
    product / databases
    product / functions
    product / storage
    on Sep 30, 2026
  2. Spandan-Mishra commented on Sep 30, 2026

    @Spandan-Mishra
    ContributorAuthor

    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:

    1. Checking if ($files === false) instead of if (! $files) so empty directory returns ([]) do not trigger an exception.
    2. Preserving the root cache directory during deleteDirectory() calls initiated by flush(), 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.

  3. piyushthawale19 commented on Oct 2, 2026

    @piyushthawale19

    Hi! I'd like to work on this issue. Could you please assign it to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / databasesproduct / functionsproduct / storage

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions