Categories
Magento Developing

How to Mass Delete Magento Products by Attribute Set

This article covers:

  1. How to delete a large number of products in Magento 1.9
  2. How to delete Magento products by attribute set

The server environment:

  1. Apache 2.4, PHP 7.1
  2. Magento 1.9.3.7

These days most Magento stores would have some sort of integration with third-party inventory solutions, and sometimes unwanted products get accidentally pushed from inventory to online store. In this case, a mass product deleting PHP script is needed.

Step 1 – Backup your database

Please make a backup of your Magento database, and fully test this script in a developing environment. This will delete products from certain attribute set. In our case, we want to delete products from the “Default” attribute set.

Step2 – Adjust your php.ini settings

To delete thousands of products, your usual php.ini setting’s maximum memory and execution time will not support this task so adjust them according. Once the task is done, make sure to revert them back to your original settings. Our examples are:

/etc/php/7.1/apache2/php.ini

max_execution_time = 3000
memory_limit = 4096M

Step 3 – Create a PHP script

Under Magento root directory we create a PHP script called delete_product.php as below:

<?php
// Load Magento in Admin mode
require("app/Mage.php");
Mage::init();
Mage::app('admin');
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

// Disable Magento indexing
function setIndexingManual($value = TRUE) {
   
    $pCollection = Mage::getSingleton('index/indexer')->getProcessesCollection();

    if($value) {
        $mode = Mage_Index_Model_Process::MODE_REAL_TIME;
    } else {
        $mode = Mage_Index_Model_Process::MODE_MANUAL;
    }

    foreach ($pCollection as $process) {
      $process->setMode($mode)->save();
    }
}
setIndexingManual(FALSE);

// Get products from 'Default' attribute set
$attrsetId = Mage::getModel('catalog/product')->getDefaultAttributeSetId();
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($attrsetId);
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addFieldToFilter('attribute_set_id', $attrsetId);

// Execute product delete
$productsName = array();
foreach($products as $product){
    $product->delete();
}

// Set Indexing back to auto
setIndexingManual();
?>

Step 4 – Run PHP script in browser to execute the delete action

Type: https://yourstoredomain.com/delete_product.php in your browser to run the script. We had about 25k products to be removed from Magento, so it took a long time. You just have to wait until current PHP script to finish or time out, don’t repeatedly refresh your PHP script!

Step 5 – Remove your PHP script after the job is done

Attention: Never run a non-tested PHP script on a production environment; and for any mass deleting/modifying work, always back up your database beforehand!

By Ethan

To many, a business is a lifetime commitment. It's easy to start one yet difficult to make it successful. Attitude, skills, experiences and dedication help hone the craft along the way, but it's often the great vision and resilience to remain focused wins the game. Read more about me here