How to flush the PHP OPcache
PHP's OPcache stores compiled PHP as bytecode in memory so it does not recompile your code on every request. That speeds up the site, but it also means that after you change PHP files the cache can keep serving the old code until it is refreshed.
Before you start
-
SSH access to the host as the application's system user - see SSH access.
-
Know whether Varnish is in front of the site: a cached page looks exactly like old code, so you need to check the response headers, not just the page.
Flush it with the TurboStack CLI
Connect over SSH and run:
tscli opcache clear
This resets the OPcache for the site's PHP-FPM, so the next requests recompile from your current files.
When you need it
-
After deploying or editing PHP files outside the normal publish flow, when your changes do not show up.
-
On sites tuned for production, where OPcache does not re-check file timestamps on every request (
validate_timestampsoff) for maximum performance - there, a flush is required to pick up new code.
Note
Right after a flush the first requests are a little slower while PHP recompiles; this settles within
moments. If you use OPcache preloading (php_opcache_preload_script), a flush does not
refresh the preloaded code. Reload PHP instead, which has no downtime:
sudo systemctl reload php8.4-fpm.service
See OPcache preloading.
Apply and verify
The flush takes effect immediately; nothing is restarted. Request a page you changed with a query string that is not in any cache, and confirm the new code answers:
curl -sI "https://www.example.com/?nocache=$(date +%s)" | grep -i -E "age|via"
An Age: 0 (or no Age header) tells you the answer came from PHP, not from Varnish.
If it did not work
Old code with Age above zero means Varnish is serving it - clear that cache too, see
Clear the Varnish cache. Old code with Age: 0 on a site that
uses preloading means the preloaded files were not refreshed: reload PHP-FPM as shown above. See
Why is my site slow?, or
contact support.
Related
- Configure PHP
- Switch PHP version
- Override PHP settings
- Why is my site slow?
- TurboStack CLI -
tscli opcache clear