We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21078
    • 77 Posts
    Hello community,

    one of my client has over 10,000 deleted resources in his MODX Revolution site and now tries to remove those. This is not really working - nothing happens after confirming the removal dialogue. Is there any other way to purge delete the resources than using the trash bin button in the manager?

    Thanks in advance!
    Chris

    This question has been answered by BobRay. See the first response.

      L.net Web Solutions
      Professional webdesign and development with MODX and WordPress.
      • 3749
      • 24,544 Posts
      The PHP default timeout is usually set to 30 seconds, so the script is probably timing out. There's a good chance that it is removing resources, just not all of them. So if you run it enough times, reloading the page after 30 seconds each time, it may get the job done.

      Here's a much better method if you have cPanel access.

      (Just to be on the safe side, I would export the modx_site_content table to a SQL file first.)

      Go into PhpMyAdmin in cPanel.
      Select the modx database
      On the SQL tab, enter this:

      DELETE FROM modx_site_content WHERE deleted=1


      Click on the "Simulate Query" button.
      It should give you the number of records that would be deleted.
      If it looks OK, close the dialog and click on the "Go" button.



        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
        • 11055 ☆ A M B ☆
        • 3,112 Posts
        @BobRay,
        Resources have many relations with other elements.
        So I believe it's better to use MODX's API to delete resource, because there are cascading actions.

        Try this script:
        <?php
        define('MODX_API_MODE', true);
        
        // this goes to the main index.php in web root
        include 'index.php';
        
        $c = $modx->newQuery('modResource');
        $c->where(array(
            'deleted' => 1
        ));
        
        // or less amount
        $c->limit(1000);
        
        $resources = $modx->getCollection('modResource', $c);
        if (!$resources) {
            die('No $resources was found');
        }
        
        foreach ($resources as $resource) {
            $resource->remove();
        }
        
        echo 'Done';
        


        Save it somewhere alongside of the main index.php.

        run it several times.

        Delete the file afterwards!
          Rico
          Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
          MODx is great, but knowing how to use it well makes it perfect!

          www.virtudraft.com

          Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

          Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

          Maintainter/contributor of Babel

          Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
          • 52064
          • 120 Posts
            FerX - Developer at Eracom s.r.l.
          • discuss.answer
            • 3749
            • 24,544 Posts
            +1 for goldsky's method. Using the SQL could leave behind some orphaned intersect objects. SiteCheck would find and remove them, but it might also time out, in which case it would have be run multiple times.

            Using getIterator will save memory, but it won't help with the timeout, and could make it worse because it will be significantly slower.

            This modification could possibly do it in one shot:

            <?php
            define('MODX_API_MODE', true);
             
            // this goes to the main index.php in web root
            include 'index.php';
            while(true) { 
                set_time_limit(0);
                $c = $modx->newQuery('modResource');
                $c->where(array(
                    'deleted' => 1
                ));
             
                // or less amount
                $c->limit(1000);
             
                $resources = $modx->getCollection('modResource', $c);
                if (!$resources) {
                    break;
                }
             
                foreach ($resources as $resource) {
                    $resource->remove();
                }
            }
            
            echo 'Done';


            It depends on whether the host has set PHP to ignore set_time_limit() or not.

            If the script were run by a one-time cron job, it would definitely not time out.


              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
              • 21078
              • 77 Posts
              Thanks a lot for you answers! I will give the PHP script a try and will report how it worked out.
                L.net Web Solutions
                Professional webdesign and development with MODX and WordPress.
                • 21078
                • 77 Posts
                Quick update: I used Rico's script in the edited version by Bob and it worked out well! It took some minutes but all resources were removed. Thank you again!
                  L.net Web Solutions
                  Professional webdesign and development with MODX and WordPress.