Clear APC Cache script for Magento updates

Imagine it’s end of Friday, you finished testing some new e-commerce feature on Magento staging, everything worked OK, you deploy it to Magento production and … nothing. You see no new behavior in production at all or only some of it. It can be worse – a 500 error. Very odd.

Possible causes: some sort of caching, cache for PHP code – APC (or it’s friends).

If APC PHP opcode cache you use for your Magento e-commerce site is really the cause you need to reset it. It’s easier said than done to remember some API you use twice a year.

APC reset script

I wrote a hassle-free bash script to clear APC cache in one command:

The script is ready to use after simple configuration of two variables:

  • DIR_ROOT should contain absolute path to the root directory of your web-server.
  • URL_ROOT is for the address of the website.

Upload the configured script to some secure place on your web-server (e.g. home directory) and run it under a user with sufficient rights to write into the root of the web-server, e.g. web-server user or root.

You can delete the script from the web-server after it successfully reset APC cache or move it to some other folder outside of the site’s root – you are likely to need it again.

Some details about the script:

  • The script creates a PHP file to call APC function to reset its cache
  • For security reasons that PHP file is created under a random name and is removed at the end.
  • The PHP file is executed by the web-server to call a cache cleaning function for APC cache shared between PHP processed spawned by web-server or PHP-FPM
  • It resets system and user APC caches.
Read more about: