Clearing the MODX Cache

Clear the MODX cache fully with the CacheClear extra


In the previous articles, we looked a ways to prevent Google Chrome from caching your code. That won't help if the problem is in the MODX cache, though. In this article we'll look at a method that reliably clears the MODX cache with a snippet.


MODX logo

The Problem

You can clear the cache in MODX with the top menu item Manage -> Clear Cache. In code, you can do it this way:

$cm = $modx->getCacheManager();
$cm->refresh();

Either way, MODX is supposed to clear the entire cache. On a number of occasions, though, I've run into situations where it seemed like the cache wasn't fully cleared. Plugins and System Settings seem to be particularly sticky. I confess that I could be wrong about this, but it's happened enough times that I don't really trust those methods to fully clear the cache. In the early MODX 2.0 era, MODX suffered from cache corruption, and clearing the cache in the Manager didn't always fix it for me.

It's also difficult to tell what has been removed when you use the methods above because MODX immediately refreshes the cache after clearing it.

If you read the MODX Forums, I'm sure you've seen the advice to manually delete all the files in the core/cache directory. It's a pain to do this since in the MODX Files page, you'd have to remove them individually. Removing them in cPanel's File Manager or using FTP is still an inconvenience. I finally wrote my own extra, CacheClear that removes the files for me. When it runs, it manually deletes all files in the cache directory, except for those in the logs directory. Several extras write logs to that directory, and that's where the MODX error log lives, so it makes sense to preserve them. Those files are never executable and are never used when loading Manager or front-end pages (except the Error Log itself), so they can't really cause any trouble.

CacheClear asks the MODX cacheManager where the cache is, so even if the cache is relocated or you're using an alternative cache scheme, it will still work.

CacheClear installs in Package Manager (Extras -> Installer). To use it, you simply view the CacheClear resource, which will report on what has been removed. It's a comfort to know that once I've run CacheClear, turned on the Cache Killer Chrome extension (described in the previous article), and reloaded a page, I know for sure that nothing was cached.


Coming Up

In the next article, we'll look at a way to make sure two users can't log in at the same time with the same MODX credentials.



Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)