All of you who use WordPress with the Autoptimize plugin will know the problem: The plugin does not automatically delete Cached files, and it has to be done manually.

Especially if you have the “Aggregate inline JS” and “Aggregate inline CSS” functions enabled, the volume of data collected in the cache file storage folder is enormous.

So the administrator is forced to manually delete these files.
Below we will see a few lines of code that if added to the functions.php of the theme you are using will do all the work for you.
We are listing the code we use at SecNews, via WPress.gr. It is configured to delete cached files when they reach 512MB in size.
//* Clear autoptimize cache at 512MB SecNews if (class_exists('autoptimizeCache')) { $secnewsMaxSize = 512000; $statArr=autoptimizeCache::stats(); $cacheSize=round($statArr[1]/1024); if ($cacheSize>$secnewsMaxSize){ autoptimizeCache::clearall(); header("Refresh:0"); } }
Of course you can change the value in $secnewsMaxSize to 1024MB if you don't want to burden your sites with ongoing php queries.
If you wish to change the size, convert 1024MB to KB aka: 1024000.
That's it! Next time you won't need to manually delete the cached files. At the end of the code as you can see there is a refresh in the header.
