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:
# This script creates a simple PHP script to clear APC cache
# in the root directory of a website and calls it via HTTP (so the script is
# executed in the same environment as PHP code of the website)
random_name=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
cat > $DIR_ROOT/$script_name<<EOF
echo apc_clear_cache() && apc_clear_cache('user');
result=$(wget --quiet -O - $URL_ROOT/$script_name)
if [[ $result == 1 ]]
echo 'Successfully reset APC cache'
echo 'Failed to reset APC cache'
The script is ready to use after simple configuration of two variables:
DIR_ROOTshould contain absolute path to the root directory of your web-server.
URL_ROOTis 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
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