We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51020
    • 670 Posts
    Hi there,
    I have a bunch of resources dotted around a site which are products from a particular manufacturer.
    The manufacturer no longer exists, so I need to remove all resources for those products.
    They are all tagged using tagger so I have a csv file containing a list of resources I want to delete, but there are over 350 of them, and wondered if there's a way to do this other than manually removing them form the database.

    Thanks
    Andy

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

      • 3749
      • 24,544 Posts
      I don't recommend removing them from the DB. That will leave behind all kinds of intersects connecting them to Templates and TVs.

      The Batcher extra will let you select them and will remove them properly.

      Depending on what the CSV file looks like, it should be possible to create a utility snippet to either load them and call $doc->remove(), or (safer) mark them as deleted and then click on the trashcan icon in the Resources tree.

      If you can create a query to select them by their tags, it would be faster and easier to get them with getCollection() and loop through them to mark them as deleted. You could use remove(), but if the script times out, you may end up with partially removed resources. I would do it like this:

      $c = $modx->newQuery('modResource');
      $c->where('tag criteria'); // This will depend on how they're tagged
      // I would add 'deleted' -> '0' to the where so you can run it repeatedly in case it times out.
      
      $docs = $modx->getCollection('modResource', $c);
      
      foreach ($docs as $doc) {
         $doc->set('deleted', '1');
         $doc->save();
      }


      If that times out, you may have to empty the trash and run it again. It might be wise to clear the cache between runs as well.
        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
        • 51020
        • 670 Posts
        Quote from: BobRay at Nov 14, 2017, 12:29 PM
        I don't recommend removing them from the DB. That will leave behind all kinds of intersects connecting them to Templates and TVs.

        The Batcher extra will let you select them and will remove them properly.

        Depending on what the CSV file looks like, it should be possible to create a utility snippet to either load them and call $doc->remove(), or (safer) mark them as deleted and then click on the trashcan icon in the Resources tree.

        If you can create a query to select them by their tags, it would be faster and easier to get them with getCollection() and loop through them to mark them as deleted. You could use remove(), but if the script times out, you may end up with partially removed resources. I would do it like this:

        $c = $modx->newQuery('modResource');
        $c->where('tag criteria'); // This will depend on how they're tagged
        // I would add 'deleted' -> '0' to the where so you can run it repeatedly in case it times out.
        
        $docs = $modx->getCollection('modResource', $c);
        
        foreach ($docs as $doc) {
           $doc->set('deleted', '1');
           $doc->save();
        }


        If that times out, you may have to empty the trash and run it again. It might be wise to clear the cache between runs as well.

        Thank you bob - I will take your advice onboard and give that a go!
          • 51020
          • 670 Posts
          Quote from: BobRay at Nov 14, 2017, 12:29 PM
          I don't recommend removing them from the DB. That will leave behind all kinds of intersects connecting them to Templates and TVs.

          The Batcher extra will let you select them and will remove them properly.

          Depending on what the CSV file looks like, it should be possible to create a utility snippet to either load them and call $doc->remove(), or (safer) mark them as deleted and then click on the trashcan icon in the Resources tree.

          If you can create a query to select them by their tags, it would be faster and easier to get them with getCollection() and loop through them to mark them as deleted. You could use remove(), but if the script times out, you may end up with partially removed resources. I would do it like this:

          $c = $modx->newQuery('modResource');
          $c->where('tag criteria'); // This will depend on how they're tagged
          // I would add 'deleted' -> '0' to the where so you can run it repeatedly in case it times out.
          
          $docs = $modx->getCollection('modResource', $c);
          
          foreach ($docs as $doc) {
             $doc->set('deleted', '1');
             $doc->save();
          }


          If that times out, you may have to empty the trash and run it again. It might be wise to clear the cache between runs as well.

          Bob - so I'm trying out your snippet idea, where I can bulk delete by tag.
          But I'm not sure what to do with your snippet. I added it to a new resource, and changed:
          $c->where('tag criteria');
          


          to:

          $c->where('mytagname');
          


          But this marked all the resources in my resource tree as deleted.
          I haven't really used utility snippets before, so sorry for being a bit thick - but how do I get this to work?

          Thanks
          Andy
            • 3749
            • 24,544 Posts
            I don't really know how tags are stored, but your query would probably have to be based on the object that stores them (rather than modResource -- my bad) and the the where would have to be something like $c->where(array('fieldName' => 'mytagname'));
              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
              • 17301
              • 932 Posts
              Do you have any other information in your CSV that could help? Batcher has a bunch of fields in the advanced search that may be helpful?
                ■ email: [email protected] | ■ website: https://alienbuild.uk

                The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
                • 51020
                • 670 Posts
                Quote from: lkfranklin at Nov 17, 2017, 07:03 AM
                Do you have any other information in your CSV that could help? Batcher has a bunch of fields in the advanced search that may be helpful?

                The CSV just has the tag and the resource ID - I had a look at batcher, but the advanced fields didn't really help, as I don't have much to work with.
                  • 3749
                  • 24,544 Posts
                  How did you create the CSV file?
                    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
                    • 17301
                    • 932 Posts
                    Well bit of a round about way but you could create a new template in modx and then use importx to sign all resources in your csv to that new template and then use batcher to select and delete all using that template.

                    Though I'm sure somehow has a much better solution lol
                      ■ email: [email protected] | ■ website: https://alienbuild.uk

                      The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
                      • 51020
                      • 670 Posts
                      Quote from: BobRay at Nov 17, 2017, 09:08 PM
                      How did you create the CSV file?

                      I exported the tagger table from the database as a csv filtered by the tag in question.