We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    I know this has been asked before, but how do you clear the MODx cache programatically? I’m working on a bash script to copy our production environment to our dev server, and one of the steps in that process is to clear the MODx cache. Can I simply delete a few files via the command line?

    I can’t just hit a page in a browser... e.g., this won’t work:
    wget http://my_dev_domain.com/manager/index.php?a=26


    because you have to be logged in... heheh.

    Thanks!
      • 25663 MODX Staff
      • 12,272 Posts
      Might start looking here. Please report in that thread if it helps you out, as that’s a very cool and interesting bit of code that may have flown under the radar:

      http://modxcms.com/forums/index.php/topic,26266.0/topicseen.html
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Thanks, Ryan. I’m unclear as to whether or not that bit of code does anything beyond removing the *.pagecache.php files in /assets/cache/ -- I can just do rm via a bash script and be done with it, but is there any magic going on when you invoke the MODx clear cache command? Are there secret database entries that are updated, etc.? I did make a post in that thread, but it unfortunately doesn’t look very active.
          • 7231
          • 4,205 Posts
          I learned that you need to update the sitePublishing.idx.php if your staging server has set publish schedules otherwise MODx will not publish them on the scheduled date on the live server. It holds the timestamp for the next scheduled publish date, if no schedule it is 0.

          note: I could be wrong about this, I reached my own conclusions on this one.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don't know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 10487 MODX Staff
            • 1,535 Posts
            Quote from: Everett at Feb 21, 2009, 12:10 PM
            I’m unclear as to whether or not that bit of code does anything beyond removing the *.pagecache.php files in /assets/cache/ -- I can just do rm via a bash script and be done with it, but is there any magic going on when you invoke the MODx clear cache command?
            Cache syncing via MODx rebuilds the sitePublishing.idx.php but also the siteCache.idx.php file - the latter being extremely important. Merely clearing out the pageCache files is not a full cache synchronisation. See this file in your MODx install: /manager/processors/cache_sync.class.processor.php
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 33712
              • 6 Posts
              According to the MODx wiki, you can call:
              $modx->clearCache()


              However, looking at what that code actually does, it’s no different than the ClearCache script -- it removes all of the *.pageCache.php files in assets/cache/ without rebuilding siteCache.idx.php

              So, in order to clear the cache, do what the manager does:

              function clearCache() {
              	include_once "manager/processors/cache_sync.class.processor.php";
              	$sync = new synccache();
              	$sync->setCachepath("assets/cache/");
              	$sync->setReport(true);
              	$sync->emptyCache();
              }
              


              This thing works like a charm; I had all kinds of trouble with programmatically-created blog posts not getting picked up by Ditto, and using this function after creating each post did the trick.

              I used that function in a snippet; adjust the paths to use elsewhere.

              Seems like a MODx bug, no?
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: _phred at Mar 04, 2009, 10:27 AM

                According to the MODx wiki, you can call:
                $modx->clearCache()


                Seems like a MODx bug, no?
                Not really, just misinformation in the wiki and a leftover, soon-to-be-deprecated function that should have more appropriately been called $modx->clearPageCache(), as it is meant to simply remove a single page from the cache. Your information on how to do this is correct.

                Revo will feature a more robust API with a very flexible cache clearing function, in addition to a wealth of additional caching features.
                  • 33712
                  • 6 Posts
                  Just added a note to the $modx->clearCache() page so that other people won’t get off track. I haven’t looked deeply enough into the Evolution core to describe the distinction between the two, other than using synccache does what the Manager does, and $modx->clearCache() doesn’t. I’ll leave it to better minds to elucidate that connection. wink

                  http://wiki.modxcms.com/index.php/API:clearCache#Related_Functions