PHP clearstatcache() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, clearstatcache() clears the cache that PHP uses to remember file information. Functions such as filesize(), filemtime(), and is_file() save their results so repeated calls are fast.

This cache can cause old values to appear after a file changes in the same script. Call clearstatcache() before you read the file information again to be sure you get the fresh value.

Syntax

Call clearstatcache() with the values you need. Start with this line.

PHP
clearstatcache();
CallWhat it does
clearstatcache()Clears the stat cache for all files.
clearstatcache(true)Clears the stat cache and the realpath cache as well.
clearstatcache(true, $file)Clears the cached data for one file only.

PHP does not cache a failed lookup, so file_exists() on a missing file is always fresh. The stat cache is only used for files that exist, and the cache is cleared automatically by functions like unlink(), rename(), and touch().

Library declarations
PHP
clearstatcache(bool $clear_realpath_cache = false, string $filename = ""): void

Parameters

ParameterDescription
clear_realpath_cacheOptional. If true, the realpath cache is cleared too. The default is false.
filenameOptional. Clear the realpath and stat cache for this file only. It is used only when the first argument is true.

Return Value

Return ValueDescription
voidThis function does not return a value.

PHP Examples

Example 1 — Fix a Stale File Size

filesize() remembers its answer. After we add text to the file, the old size is still returned until the cache is cleared.

PHP
<?php
$file = __DIR__ . "/demo.txt";
file_put_contents($file, "Hello");

echo filesize($file), PHP_EOL;

file_put_contents($file, " World", FILE_APPEND);
echo filesize($file), PHP_EOL;

clearstatcache();
echo filesize($file), PHP_EOL;

unlink($file);

How It Works

  1. The file starts with Hello, so the first size is 5.
  2. We append World, but filesize() still returns the cached 5.
  3. clearstatcache() removes the saved information.
  4. The next filesize() call reads the disk again and returns 11.

Example 2 — Clear the Cache for One File

Pass true and a file name to clear the cache only for that file.

PHP
<?php
$file = __DIR__ . "/demo.txt";
file_put_contents($file, "abc");

echo filesize($file), PHP_EOL;

file_put_contents($file, "defgh", FILE_APPEND);

clearstatcache(true, $file);
echo filesize($file), PHP_EOL;

unlink($file);

How It Works

  1. The file contains 3 bytes and filesize() caches that value.
  2. We append 5 more bytes to the same file.
  3. clearstatcache(true, $file) clears the data for that file only.
  4. filesize() now returns the new total of 8.

Example 3 — Watch a File Grow in a Loop

When you check a file again and again, call clearstatcache() inside the loop so each check is fresh.

PHP
<?php
$file = __DIR__ . "/log.txt";
file_put_contents($file, "");

for ($i = 1; $i <= 3; $i++) {
    file_put_contents($file, "abc", FILE_APPEND);

    clearstatcache();
    echo "Round ", $i, ": ", filesize($file), " bytes", PHP_EOL;
}

unlink($file);

How It Works

  1. We start with an empty file.
  2. Each round appends 3 bytes with FILE_APPEND.
  3. clearstatcache() makes sure the size is not taken from the cache.
  4. The printed size grows by 3 bytes in each round.

Frequently Asked Questions

It clears the cached file information that PHP keeps for functions like filesize(), filemtime(), and is_file(). After that, those functions read fresh data from the disk.
Call it when a file changes during the same script and you check its size, time, or permissions again. Without it you may see old values.
Not for missing files, because PHP does not cache failed lookups. It can be stale for a file that exists and then is removed by something outside PHP.
Yes. Functions like unlink(), rename(), touch(), chmod(), and chown() clear the cached data for the file they change.
Only a little. Use it only where you really need fresh data, because the cache exists to make repeated checks faster.

Next: copy()

copy() makes a copy of a file. The original file stays where it is, and a new file with th.

copy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful