We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36551
    • 416 Posts
    I've a got a couple of very large sites that need some serious housekeeping. Over the years, my clients have uploaded hundreds images, and PDFs, many of which are no longer used. I need a way to identify and remove all the orphan files. I searched through the Revo extras but didn't find anything that would help.

    I found this in the forum from 9 years ago for Evo.

    https://forums.modx.com/index.php?action=thread&thread=32240&i=1

    Something like this would do the trick. I just need a Revo version. Does anyone have something like this they could share? An alternate approach or tool?
      • 3749
      • 24,544 Posts
      That old version was very inefficient. Here is one using a prepared statement that only makes one massive query per search term. You need to install the DirWalker extra first. It's very fast, but if the site is huge, it could time out. I would recommend renaming the orphaned images with an 'ORPHAN' prefix and waiting a while to delete them.

      Note: This script may identify thumbnails as orphans when they're not.

      Steps:

      1. Install the DirWalker extra
      2. Create a resource with this tag:
         [[!OrphanImages]]

      3. Create a snippet called OrphanImages with the code section below.
      4. View the resource
      5. Donate at Bob's Guides wink

      /* OrphanImages snippet */
      
      /* Modify the next line as necessary; the search is recursive, so it will cover the dir.
         specified, and all directories under it */
      
      $imageDirectory = MODX_ASSETS_PATH . 'images';
      
      require_once MODX_CORE_PATH . 'components/dirwalker/model/dirwalker/dirwalker.class.php';
      
      /* Get all the files */
      $dw = new DirWalker();
      $dw->dirWalk($imageDirectory, true);
      $files = $dw->getFiles();
      
      /* Create Query */
      $objects = array(
          $modx->getTableName('modResource') => 'content',
          $modx->getTableName('modChunk') => 'snippet',
          $modx->getTableName('modSnippet') => 'snippet',
          $modx->getTableName('modPlugin') => 'plugincode',
          $modx->getTableName('modTemplate') => 'content',
          $modx->getTableName('modTemplateVarResource') => 'value',
          $modx->getTableName('modTemplateVar') => 'default_text',
      );
      
      $string = '';
      $s = array();
      foreach($objects as $table => $fieldName) {
          $s[] =  "(SELECT $fieldName FROM $table WHERE $fieldName LIKE :s)";
      }
      
      $string = implode(' UNION ', $s);
      
      /* Prepare Query and bind $s param */
      $sql = $modx->prepare($string);
      $sql->bindParam("s", $searchTerm);
      
      
      /* Execute query once for each file */
      $output = '';
      foreach($files as $path => $searchTerm) {
          $searchTerm = "%" . $searchTerm . "%";
          $sql->execute();
          $count = $sql->rowCount();
          $msg = $count? 'IN USE ' : '<span style="color:red">ORPHAN </span>';
          $output .= "\n<br>" . $msg . $path . " Count: " . $count;
      }
      return $output;
      
      [ed. note: BobRay last edited this post 8 years, 6 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36551
        • 416 Posts
        Bob

        Wow! Thank you so much! I will give this a try later today.
          • 36551
          • 416 Posts
          Bob

          I ran this on one of my sites. This IS very fast! It reported 547 orphaned files and 1 in use. I spot checked a few and found images used on the site were listed as orphaned. I suspect it listed virtually all as orphaned except for one, which I believe is an orphan.

          I put the snippet on a page and viewed the page. Is there anything else I should have done?

          Will this find images specified in CSS as background-images? If not, no big deal. Just curious.

          Thanks for this!
            • 3749
            • 24,544 Posts
            You did it right. I don't have enough links and images on my dev. site to test it properly, so maybe the query is flawed. (or maybe the test is backwards). On my site, it flags about half the images as orphans.

            It wouldn't find background images because it doesn't know about the CSS files. That would take another section of code.

              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 36551
              • 416 Posts
              Bob

              I can give you access to the site if that would helpful.
                • 3749
                • 24,544 Posts
                Ah. In that last section, add the extra line at the beginning:

                foreach($files as $path => $searchTerm) {
                    $searchTerm = "%" . $searchTerm . "%"; // add this line
                    $sql->execute();
                    $count = $sql->rowCount();
                    $msg = $count? 'IN USE ' : '<span style="color:red">ORPHAN </span>';
                    $output .= "\n<br>" . $msg . $path . " Count: " . $count;
                }
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 36551
                  • 416 Posts
                  Much Better!

                  Now it identified 395 In Use and 152 Orphans.

                  Perfect!