Programmatically delete all Magento attribute sets

You’re developing a Magento importer and you want to clean your DB after some tests? Here you have:

require_once dirname(__FILE__) . '/app/Mage.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$resource = Mage::getSingleton('core/resource');
$db_read = $resource->getConnection('core_read');

$attribute_sets = $db_read->fetchCol("SELECT attribute_set_id FROM " . $resource->getTableName("eav_attribute_set") . " WHERE attribute_set_id<> 4 AND entity_type_id=4");
foreach ($attribute_sets as $attribute_set_id) {
	try {
		Mage::getModel("eav/entity_attribute_set")->load($attribute_set_id)->delete();
	} catch (Exception $e) {
		echo $e->getMessage() . "\n";
	}
}

Note that only products’ attribute sets are deleted, those are often generated automatically during imports.

3 thoughts on “Programmatically delete all Magento attribute sets

  1. Vivek

    Removed attribute set using your code, but it creates issues in category url, they redirect to 404 page.
    Also in admin panel it the category tabs not displayed in Catalog >> Manage Categories section.

Leave a Reply

Your email address will not be published. Required fields are marked *