Standalone Script for Clearing APC Cache

After upgrading WordPress to a new version, you might need to clear the APC opcode cache. Here is a simple script that you can store in the root of your website, for example, clearapc.php:

if (function_exists('apc_clear_cache') && $_GET['pass'] == 'secret') {
        if (apc_clear_cache() && apc_clear_cache('user'))
                print 'All Clear!';
        else
                print 'Clearing Failed!';

        print '<pre>';
        print_r(apc_cache_info());
        print '</pre>';
} else {
        print 'Authenticate, please!';
}

Then call the script via http://example.com/clearapc.php?pass=secret

Instead of using ?pass=secret, you might well call the filename something that is hard to guess: clearapc93920.php

Use Contact Form 7 to collect business leads and enquiries? I created Storage for Contact Form 7 plugin which stores them safely in WordPress database.

Get it now for only $19 →

4 Comments

  1. Ryan Hellyer says:

    I would have thought there would be a hook somewhere in the upgrade script which could be fired automatically instead.

  2. Ryan Hellyer says:

    I stumbled back on this post whilst Googling searching for how to clear APC. Thanks for the handy snippet! I spent a few hours trying to debug something this morning until realising it was the damed object cache causing the problems after a fresh database dump.

    One change I might make if I get time, is to do the authentication via a form instead of the URL. It’d be slightly more secure that way. It’s a minor issue though since the worst case scenario is that someone can clear your cache.

  3. Wayne says:

    Thanks so much for this!
    After trying to figure out why an install directory name change caused WordPress to crash, I finally got some idea that it had to do with a php caching issue of some kind. Include calls were trying to use the old path. This cleared it right up :)

Leave a Reply