Return to Snippet

Revision: 39054
at January 12, 2011 22:03 by TheJasonParker


Initial Code
<?php
 
ob_end_flush();
echo "Started ".date("d/m/y h:i:s")."
";
define('MAGENTO', "/domains/example.com/http");
 
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
 
$products = Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('data_set', 1544);
 
$sql = ""; $undoSql = "";
for ($i=0; $i<=8; $i++) {
$sql .= "UPDATE index_process SET mode = 'manual' WHERE index_process.process_id =$i LIMIT 1;";
$undoSql .= "UPDATE index_process SET mode = 'real_time' WHERE index_process.process_id =$i LIMIT 1;";
}
 
$mysqli = Mage::getSingleton('core/resource')->getConnection('core_write');
$mysqli->query($sql);
 
$total_products = count($products);
$count = 0;
foreach($products as $product){
$product->delete();
 
if($count++%100 == 0) {
$cur = strtotime(date("d/m/y h:i:s")) - $time;
$time = strtotime(date("d/m/y h:i:s"));
echo round((($count/$total_products)*100),2)."% deleted ($count/$total_products) ".round(100/$cur)." p/s ".date("h:i:s")."
";
flush();
}
}
 
echo "Ended ".date("d/m/y h:i:s")."
";
$mysqli->query($undoSql);
exit();
 
?>

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

Initial Description
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.

Initial Title
Mass delete products for Magento 1.4+

Initial Tags
magento

Initial Language
PHP