How To Flush PHP Opcache?

flush opcache

Do you want to flush PHP Opcache?

It depends on how you are using PHP. However, in this post, you will know how to clear the PHP Opcache.

  1. Find the PHP Method
  2. CGI or FastCGI Method
  3. CLI Method
  4. Mod_php Method
  5. PHP-FPM Method

Find the PHP Method

There are multiple ways to run PHP. With every update, PHP is getting better and offering more and more. There is a CGI way of running, FastCGI, mod_php, and PHP-FPM method.

If you want to flush the PHP, you will have to know the PHP method first; then, you can use a suitable way to clear the OPcache.

However, if you want a general method to flush your Opcache, then you can create a new PHP file called flush_cache.php in the docroot.

Flush_cache.php:

<?php
opcache_reset();
?>

Whenever you want to flush your Opcache, just browse to the flush_cache.php file. The file will call the ocache_reset() for the entire Opcache.

The site will be populated with the cache again with the new PHP request.

One thing to keep in mind is that you should call the URL in the same way you open your website: HTTP or HTTPS

And you don’t have to worry about the running process, as the execution of the flush_cache.php file does not affect the running processes.

This is the general method, but for a more distinctive approach, keep reading.

CGI or FastCGI Method

If you are running PHP as CGI or FastCGI, there is no point in clearing the OPcache.

Because the cache is already flushing at every request, a new php-cgi process starts with every request when the PHP is running as CGI.

On the other hand, if you force the Opcache for a CGI or FastCGI model, it will hurt the store’s overall performance.

Every request will store the Opcache in the FastCGI process, but that cache is not beneficial as it will destroy the moment the new process begins. It means the Opcache is stored, but there is no way to utilize it. However, the process is eating the CPU.

That’s why CGI is not a recommended way to run PHP.

CLI Method

CLI is kind of similar to CGI when it comes to the Opcache. It will hurt the performance of the site.

First of all, the command line does not have any Opcache. Once you enable it, the PHP will try to store the Opcache in the memory. But then as soon as the CLI commands end, the cache will be dead too.

However, if you want to flush PHP opcache on CLI, you only have to restart the PHP commands. 

Simply do the CTRL + C to abort the commands, and start it again.

mod_php Method

You can reload or restart your apache webserver to flush to Opcache is mod_php method.

$ apachectl graceful
$ service httpd reload

A reload will simply clear the Opcache in PHP, while a restart will also do similar work. However, a restart will kill all of the active HTTP connections.

PHP-FPM Method

To flush PHP Opcache on the PHP-FPM method, you have to send a reload to your PHP-FPM daemon. The reload will clear the Opcache, and when the next request arrives, it will force it to rebuild the cache.

$ service php-fpm reload

You can flush the entire cache of all the websites by reloading the single master. The single master will reset the master’s Opcache, and flush the entire cache.

However, tools like cachetool can give you more control over your command line. The cachetool will connect to your PHP-FPM socket, and send the commands similar to the webserver.

For that, first, download the phar to manipulate the cache.

$ curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar

Next, use that phar to send commands to your PHP-FPM daemon.

$ php cachetool.phar opcache:reset --fcgi=127.0.0.1:9000
$ php cachetool.phar opcache:reset --fcgi=/var/run/php5-fpm.sock
flush php opcache

Summary

PHP Running MethodHow to Flush PHP Opcache?
General MethodCreate flush_cache.php in the docroot and browse it
CGI or FastCGI MethodCache is already flushing at every request
CLI MethodRestart the PHP commands (CTRL + C)
mod_php MethodReload or restart your apache webserver
PHP-FPM MethodSend a reload to your PHP-FPM daemon

Also Read:

Wrapping Up

Opcache is a fantastic way to improve the performance of the site. It stores the precompiled script in shared memory, so there is no need for PHP to load and parse scripts every time.

But sometimes, Opcache does not refresh the updated files in the cache. For that, you have to flush the cache, so it can generate the updated cache.

It is vital for the eCommcerce stores using PHP based platforms.

In this post, I mentioned the ways to flush Opcache, for all the PHP running methods. Check the post on clear WordPress cache.

The ServerGuy customers can request our support team to do this for them. 

If there is any problem with any step, leave it in a comment.

Latest Magento Tips, Guides, & News

Stay updated with new stuff in the Magento ecosystem including exclusive deals, how-to articles, new modules, and more. 100% Magento Goodness, a promise!

1 thought on “How To Flush PHP Opcache?”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

We can help you. Right now.

Fast growing merchants depend ServerGuy for high-performance hosting. Experience counts. Let's get started.

Talk to a sales representative

USA / Worldwide

+1.714.2425683

India

+91.9852704704

Core Web Vitals Book COver

Is your website ready for Core Web Vitals?

Take this FREE book with you and optimize your store for speed.

Learn all about new Google new ranking factors and get that top ranking.