Clear APC cache in PHP

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, November 16, 2014

Follow me on Twitter as @mattiasgeniar

How do you clear the APC cache? There are basically two methods: as a PHP developer, you can use the built-in PHP functions – or as a SysAdmin, you can restart the necessary services to flush the APC cache.

As a PHP developer

You can call the apc_clear_cache() function to clear the cache. To clear the user cache (key/value), you can use apc_cache_clear('user'). To clear the system cache, the one that holds the byte-code of the PHP files (the so called “opcode” cache), just call apc_cache_clear() without options.

As a System Administrator

This depends on the way you run your PHP application.

With Apache + mod_php

As PHP is run as an Apache module, it’s sufficient to reload the Apache service. A restart is not needed (but it will also work), a reload is sufficient.

$ service httpd reload
$ service apache2 reload

With PHP-FPM

This can be used with Nginx, Lighttpd or even Apache. If your PHP server is running as a daemon via PHP-FPM, you can reload PHP-FPM.

$ service php-fpm reload

If this is how you’re running your PHP stack, you may consider using multiple PHP-FPM masters as outlined in “A better way to run PHP-FPM”, as it gives you an APC cache per PHP-FPM pool you are running.

As the built-in PHP server

If you start your PHP daemon via the built-in server, as php -S 127.0.0.1:80, you need to restart your daemon. That probably means sending a kill signal to the PHP process and starting it anew.

PHP as FastCGI

Not to be confused with running PHP as the PHP-FPM daemon, in the older days PHP was run as a FastCGI script. There’s no need to clear the APC cache, as it gets invalidated on every new request – each request starts a new process. In fact, you’re better of disabling APC altogether, as it produces overhead that never gives you the benefits.

Want to flush the Opcache instead of APC? Check out this article: How to clear PHP’s Opcache.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.