I was working on the correct deployment of Magento2 shops via Bamboo. Unfortunately I saw a lot of bad examples on how to clear or purge / clean your PHP OpCache. Most of them recommend using a simple .php file with a the clear/purge function in it. C’mon, that is just bad-practise.
Magento2 clearing the php opcache
The correct way; Honestly it should be protected from the “outside”. You do not want a client or a bot hitting your page clear page. Obviously there are a lot of ways to protect this. For example a nginx configuration to skip that file, or placing the php file outside the webfolder.
Let’s make it good
I wanted to do better. Magento2 has a nice CLI option which we can ‘extend’. I wanted the end result to be useful, stable and mature. Therefor I created an extension. It is easy (installable via packagist, composer), stable and IMO the correct way to add these features. Not only can I provide an option to clear the Magento2 php OpCache via the CLI but also via the admin backend.
Magento2 php Opcache manager
As you can see it has an extra option in the additional cache management. This is just useful if you want to flush it without server access.
The CLI gives 2 options:
php bin/magento opcache:status
This returns an array of the PHP OpCache status and
php bin/magento opcache:clear
which clears the cache.
Installing via composer
It is just as simple as
composer require webfixit/opcache
Obviously you can check out the entire source code (which is open source!) here: https://github.com/wiardvanrij/m2-opcache-manager