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

    I'm more a frontend person, so this task is just a bit to much for my PHP knowledge:
    I'm using the importX plugin (https://rtfm.modx.com/extras/revo/importx) to import a CSV file. Each line of the file creates a new resource/page. That's nice and exactly what I want.
    But now the problem starts: to keep the index clean of old resources/pages, i'd like to delete all child pages before I import an update.
    So my question is:
    How can I delete child pages using/building a plugin. Or can I put some lines of code into the importX plugin?

    Thanks for helping me!

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      A snippet with this code would delete all direct children of the resource with the ID 12. You'd could put the snippet tag on an unpublished page and visit that page to delete the children:

      $docId = 12;
      $docs = $modx->getCollection('modResource', array('parent' => $docId));
      $count = count($docs);
      foreach ($docs as $doc) {
          $doc->remove();
      }
      
      return 'Removed ' . $count . ' resources.';

        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
        • 51299
        • 3 Posts
        Thank you, BobRay!
        Exactly what I was looking for! It's up and working like a charm!
          • 3749
          • 24,544 Posts
          I'm glad I could help. smiley

          BTW, if you would rather specify the parent's ID in the snippet tag, you can do this:

          [[!MySnippet? &docId=12]]


          /* the 12 here is a the default parent ID to use if no property is sent */
          $docId = $modx->getOption('docId', $scriptProperties, 12, true); 
          $docs = $modx->getCollection('modResource', array('parent' => $docId));
          $count = count($docs);
          foreach ($docs as $doc) {
              $doc->remove();
          }
           
          return 'Removed ' . $count . ' resources.';


          If you have multiple parents to remove children of, you can do this:

          [[!MySnippet? &docId=12,22,34]]



          $docIds = explode(',', $modx->getOption('docId', $scriptProperties, '', true)); 
          
          $total = 0;
          foreach ($docIds as $docId) {
              $docs = $modx->getCollection('modResource', array('parent' => $docId));
              $total += count($docs);
              foreach ($docs as $doc) {
                  $doc->remove();
              }
          }
           
          return 'Removed ' . $total . ' resources.';


            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
            • 51299
            • 3 Posts
            Quote from: BobRay at Oct 26, 2015, 12:35 PM


            If you have multiple parents to remove children of, you can do this:

            [[!MySnippet? &docId=12,22,34]]



            Ah! That's nice smiley
            I came up with step one by myself. But as I have 2 parent resources whose children have to be deleted at the same time, I ended up building 2 hidden pages and passing the parent id in a template variable. This will make it even more comfortable.
            Thanks BobRay!