clearstatcache

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

Clears PHP's per-path cached file status (the stat family): long-running processes need it before re-querying a path changed externally; modern PHP already auto-invalidates most in-script writes.

C C23

C23
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h> // unlink()

int main(void) {
    // POSIX stat() always hits the kernel - there is no userspace stat cache to clear
    FILE *w = fopen("stat_demo.txt", "w");
    fputs("12345", w);
    fclose(w);

    struct stat st;
    stat("stat_demo.txt", &st);
    printf("size: %lld\n", (long long)st.st_size);
    stat("stat_demo.txt", &st); // fresh by construction
    printf("size again: %lld\n", (long long)st.st_size);
    unlink("stat_demo.txt");
    return 0;
}
Output
size: 5
size again: 5

C++ C++26

C++26
#include <filesystem>
#include <fstream>
#include <iostream>

namespace fs = std::filesystem;

int main() {
    // std::filesystem queries the OS on every call - no cache involved
    {
        std::ofstream w("stat_demo.txt");
        w << "12345";
    }
    std::cout << "size: " << fs::file_size("stat_demo.txt") << "\n";
    std::cout << "size again: " << fs::file_size("stat_demo.txt") << "\n";
    fs::remove("stat_demo.txt");
}
Output
size: 5
size again: 5

C# v11.0.100

v11.0.100
// File.* statics re-query on every call; note FileInfo CACHES per instance - call Refresh()
using System.IO;

File.WriteAllText("stat_demo.txt", "12345");
Console.WriteLine("size: " + new FileInfo("stat_demo.txt").Length);
Console.WriteLine("size again: " + new FileInfo("stat_demo.txt").Length);
File.Delete("stat_demo.txt");
Output
size: 5
size again: 5

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	// os.Stat is a fresh syscall on every call
	os.WriteFile("stat_demo.txt", []byte("12345"), 0644)
	fi, _ := os.Stat("stat_demo.txt")
	fmt.Printf("size: %d\n", fi.Size())
	fi, _ = os.Stat("stat_demo.txt") // fresh again
	fmt.Printf("size again: %d\n", fi.Size())
	os.Remove("stat_demo.txt")
}
Output
size: 5
size again: 5

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws Exception {
        // Files.size is a fresh query; only a held BasicFileAttributes snapshot can go stale
        Path p = Path.of("stat_demo.txt");
        Files.writeString(p, "12345");
        System.out.println("size: " + Files.size(p));
        System.out.println("size again: " + Files.size(p));
        Files.delete(p);
    }
}
Output
size: 5
size again: 5

JavaScript v26.13.1

v26.13.1
// fs.statSync queries the kernel every time - no cache layer
const fs = require("node:fs");
fs.writeFileSync("stat_demo.txt", "12345");
console.log("size:", fs.statSync("stat_demo.txt").size);
console.log("size again:", fs.statSync("stat_demo.txt").size);
fs.unlinkSync("stat_demo.txt");
Output
size: 5
size again: 5

PHP v8.6.0

v8.6.0
// PHP caches stat-family results per path for the script's lifetime;
// clearstatcache() drops that cache (vital after out-of-process changes)
$f = tempnam(sys_get_temp_dir(), "stat-");
file_put_contents($f, "12345");
echo "size: ", filesize($f), "\n"; // result cached for this path
clearstatcache(); // drop the cached stat entries
echo "size again: ", filesize($f), "\n";
unlink($f);
Output
size: 5
size again: 5

Python v3.15.2

v3.15.2
# os.stat is a fresh syscall on every call - no cache to clear
import os

with open("stat_demo.txt", "w") as f:
    f.write("12345")
print(f"size: {os.stat('stat_demo.txt').st_size}")
print(f"size again: {os.stat('stat_demo.txt').st_size}")
os.unlink("stat_demo.txt")
Output
size: 5
size again: 5

Rust v1.99.1

v1.99.1
use std::fs;

fn main() {
    // fs::metadata queries the OS every time
    fs::write("stat_demo.txt", "12345").unwrap();
    println!("size: {}", fs::metadata("stat_demo.txt").unwrap().len());
    println!("size again: {}", fs::metadata("stat_demo.txt").unwrap().len());
    fs::remove_file("stat_demo.txt").unwrap();
}
Output
size: 5
size again: 5

TypeScript v26.13.1

v26.13.1
// fs.statSync queries the kernel every time - no cache layer
import * as fs from "node:fs";
fs.writeFileSync("stat_demo.txt", "12345");
console.log("size:", fs.statSync("stat_demo.txt").size);
console.log("size again:", fs.statSync("stat_demo.txt").size);
fs.unlinkSync("stat_demo.txt");
Output
size: 5
size again: 5

Language differences

C: no cache - stat hits the kernel directly; the cache is a PHP userspace optimisation, not OS behaviour.

C++: std::filesystem queries every time; only a held file_status snapshot can go stale.

C#: File statics re-query, but FileInfo/DirectoryInfo cache ALL attributes on first access - call Refresh(); the closest counterpart to PHP's pitfall.

Go: os.Stat is fresh every time; a held FileInfo value is a snapshot.

Java: Files.size/readAttributes are fresh; a BasicFileAttributes object is a snapshot.

JavaScript: fs.statSync has no cache; fs.watch is a separate event mechanism.

PHP: 20+ affected functions including file_exists/filesize/filemtime/fileperms/is_file; the two arguments clear only the realpath cache or a single file; note modern PHP auto-invalidates the cache for in-script chmod/touch/fwrite writes - the classic stale scenario is a long-running process whose file is changed externally.

Python: os.stat has no cache; pathlib.Path.stat() re-queries every time.

Rust: fs::metadata has no cache.

TypeScript: same as JavaScript.