Auto clear Autoptimize cache before it gets too large

 In Functions, WordPress Plugins

Autoptimize plugin cache size can grow out of control rapidly before you can notice and that can become a huge problem for your WordPress site. The code below will schedule cron jobs to delete every week and month.

namespace wppress;
class GarbageCollection
    public function __construct()
        add_filter('cron_schedules', function ($schedules) {
            $schedules['weekly'] = array(
                'interval' => 604800,
                'display'  => __('Once a Week'),
            $schedules['monthly'] = array(
                'interval' => 2635200,
                'display'  => __('Once a month'),
            return $schedules;
        if (!wp_next_scheduled('wppress/garbage/clear')) {
            wp_schedule_event(time(), 'monthly', 'wppress/garbage/clear');
        add_action('wppress/garbage/clear', [ &$this, 'clear']);
    public function clear()
        if (class_exists('\autoptimizeCache')) {
            /* Clear autoptimize cache once a month */
new GarbageCollection();

Recent Posts

Leave a Comment

Start typing and press Enter to search

Let's work together

I'd love to work with you! Please fill out my project form to tell me more about your project/work. I reply to all enquiries within 48 hours.
Or call me on 07930 194000.


  • April - Booked
  • May - Limited
  • June - Limited


  • My day rate is £300.
  • I can provide a fixed price for your work.
  • Projects start at around £2,000.

Enquiry Form