i want to delete a product attribute from eav table pragmatically. I was getting a code which was delete customer attribute.

I want just like this code ,which was delete product attribute.

error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

    try {
        $custAttr = 'user_name';  // here enter your attribute name which you want to remove

        $setup->removeAttribute('customer', $custAttr);
        echo $custAttr." attribute is removed";
        catch (Mage_Core_Exception $e) {
        $this->_fault('data_invalid', $e->getMessage());


please help me.

6 Answers 6


It is my view that this sort of thing should be done via set-up script or if that is not possible then via the admin section. I cannot think of a situation where you need to write a script for this, but maybe there is one.

For a set-up script it is incredibly simple:

// Remove Product Attribute
$this->removeAttribute('catalog_product', 'product_attribute_code');
// Remove Customer Attribute
$this->removeAttribute('customer', 'customer_attribute_code');

If you need something more complex like removing groups or attribute sets that can also be done via a script too.

  • david ,i want to remove customer eav attribute
    – Amit Bera
    Commented May 25, 2014 at 1:33
  • 1
    @AmitBera I have added the customer removal code also, but your question might need updating to match these requirements. Commented May 25, 2014 at 5:11
  • If you are in a custom model and not extending from catalog, it's better if the resource setup is defined :) $installer = new Mage_Eav_Model_Entity_Setup('eav_setup'); Commented Aug 26, 2017 at 4:17
  • @DavidManners you should not use startSetup, because this will disable foreign key checks. Hence, the attribute will be deleted, but all associated attribute values will be kept in the database.
    – Simon
    Commented Oct 8, 2018 at 8:19

You can try below code

Mage::getModel('catalog/product_attribute_set_api')->attributeRemove($attributeId, $attributeSetId);

It will remove attribute from attribute set only but I am not sure if it will also remove it permanently.
For removing an attribute permanently, you can try below.


Other method is :
First create a test.php in magento root directory
Place below code there

error_reporting(E_ALL | E_STRICT);

require_once './app/Mage.php';

$attr = 'test_remove'; //attribute code to remove

$setup = Mage::getResourceModel('catalog/setup', 'core_setup');
try {
    $setup->removeAttribute('catalog_product', $attr);
    echo $attr . " attribute is removed";
} catch (Mage_Core_Exception $e) {
  • Anshu,is attribute set id needed ???
    – Amit Bera
    Commented Apr 7, 2014 at 12:42
  • Yes, otherwise it will break in between and give error. Commented Apr 7, 2014 at 12:45
  • I would not use startSetup() (and endSetup()) because it would disable foreign key checks and basically prevent cascading deletions in related tables. You would end up with a lot of entries in your various EAV tables that are no longer matched with an attribute. This would clutter your database. Commented Nov 17, 2017 at 8:57

With following code, it will permanently delete product attribute.

$Attributes = array('accessories_size','accessories_type','apparel_type','author_artist','bag_luggage_type','bedding_pattern','bed_bath_type','books_music_type','camera_megapixels',

        $entityType = 'catalog_product';

        foreach($Attributes as $index => $attrCode){

                $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode($entityType,$attrCode);   // it return false when attribute not exist.

  • Could you please explain your code and explain how this will help solve the OP's problem/question?
    – 7ochem
    Commented Feb 4, 2016 at 10:02
  • Sure. I have shown many attribute code in the array. what you have to do is ensure $entityType = 'catalog_product'; which tell the product_attributes not the customer attributes. In the foreach loop go through one by one and get the attribute ID using the resource model. If the Attribute not exist it will return false. If exist then just call the remove method with the attributeID for the catalog/product_attribute_api model.
    – Rani Das
    Commented Feb 4, 2016 at 10:05

You could do this using direct SQL.

The first step would be to identify what your attribute's id is, you can do this by navigating through CatalogAttributesManage Attributes, then searching for your attribute and clicking onto it to edit.

Once you are on the edit screen, you'll be able to see your attribute id in the URL of the page, something like


You also need to know what kind of attribute it is, so that you can select the correct table. The EAV tables would be along the lines of:

  • catalog_product_entity_varchar
  • catalog_product_entity_datetime
  • catalog_product_entity_int
  • catalog_product_entity_decimal, etc.

You may want to double check by using something like PHPMyAdmin and finding the attribute itself in one of those tables first.

With all of that in mind, set up a script in your root html directory, called 'delete_my_attribute.php' or something along those lines:

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
Mage::register('isSecureArea', 1);

$attribute_id = x;
$table_name = y;
$connection = Mage::getSingleton(çore/resource')->getConnection('core_write');
$sql = 'DELETE FROM ' . $table_name . ' WHERE `attribute_id` = ?';
$connection->query($sql, $attribute_id);
$sql = 'DELETE FROM `catalog_eav_attribute` WHERE `attribute_id` = ?';
$connection->query($sql, $attribute_id);
echo "Deleted successfully!";

And voila, you'll be all set. The first statement clears all of the values for any products which may have it set, and the second statement deleted the attribute from the table itself.

You'll just need to point your browser to the script for it to run.


If you get the Deleted successfully! message you should be all set ;-)


You can delete attributes via a script that is run in the browser, for example : removeattributes.php.

You can put it in your Magento root folder.

Example of running it : https://www.yourwebsite.com/removeattributes.php

The contents of the script are as follows:

include 'app/Mage.php';
$setup = Mage::getResourceModel('catalog/setup','catalog_setup');
$setup->removeAttribute('catalog_product','attribute_code'); //attribute to be deleted 

or it can be through mysql upgrade

Example :

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');


$installer->removeAttribute('catalog_product', 'attribute_code'); //attribute to be deleted




example :


Note : check your module xml for your module version

in this example :


Tested and working well in Magento and Magento


I use this script to remove attributes of given entity types in url. This script ensures an admin must be logged in before performing any attribute removal requests.

url example:



error_reporting(E_ALL | E_STRICT);

require_once './app/Mage.php';

    Mage::getSingleton('core/session', array('name'=>'adminhtml'));
        exit('Please login as admin before processing further');

    $msg = null;
    $entityType = Mage::app()->getRequest()->getParam('entity_type');
    $attributes = explode(',', Mage::app()->getRequest()->getParam('attributes'));

    if(empty($entityType)) throw new Exception('Please provide valid <u>entity_type</u> param');

    foreach($attributes as $attrCode){
        if(empty($attrCode)) throw new Exception('Please provide valid <u>attributes</u> param');

        $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, $attrCode);

            $msg .= 'Attribute <u>' . $attribute->getAttributeCode().'</u> was removed<br>';
}catch(Exception $e){
    exit('<span style="color:#ff0000">There is an exception: ' . $e->getMessage().'</span>');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.