Mass delete products for Magento 1.4+


/ Published in: PHP
Save to your folder(s)

The easiest way to pull this off is by means of a quick script. The key to this script is that the indexing functionality is disabled entirely for the process, then enabled again at the end – requiring a manual update at the end.

In our case, we filtered the type of products by its “Data Set” – an attribute assigned to that batch of products, but you can change this to suit any means of filtering.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. echo "Started ".date("d/m/y h:i:s")."
  4. ";
  5. define('MAGENTO', "/domains/example.com/http");
  6.  
  7. require_once MAGENTO . '/app/Mage.php';
  8. umask(0);
  9. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  10.  
  11. $products = Mage::getModel('catalog/product')
  12. ->getCollection()
  13. ->addFieldToFilter('data_set', 1544);
  14.  
  15. $sql = ""; $undoSql = "";
  16. for ($i=0; $i<=8; $i++) {
  17. $sql .= "UPDATE index_process SET mode = 'manual' WHERE index_process.process_id =$i LIMIT 1;";
  18. $undoSql .= "UPDATE index_process SET mode = 'real_time' WHERE index_process.process_id =$i LIMIT 1;";
  19. }
  20.  
  21. $mysqli = Mage::getSingleton('core/resource')->getConnection('core_write');
  22. $mysqli->query($sql);
  23.  
  24. $total_products = count($products);
  25. $count = 0;
  26. foreach($products as $product){
  27. $product->delete();
  28.  
  29. if($count++%100 == 0) {
  30. $cur = strtotime(date("d/m/y h:i:s")) - $time;
  31. $time = strtotime(date("d/m/y h:i:s"));
  32. echo round((($count/$total_products)*100),2)."% deleted ($count/$total_products) ".round(100/$cur)." p/s ".date("h:i:s")."
  33. ";
  34. }
  35. }
  36.  
  37. echo "Ended ".date("d/m/y h:i:s")."
  38. ";
  39. $mysqli->query($undoSql);
  40. exit();
  41.  
  42. ?>

URL: http://www.sonassi.com/knowledge-base/magento-knowledge-base/mass-delete-products-in-magento/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.