7

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.

<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$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

23

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:

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

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

4
  • 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
7

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.

Mage::getModel('catalog/product_attribute_api')->remove($attributeId);


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

<?php
error_reporting(E_ALL | E_STRICT);

require_once './app/Mage.php';
umask(0);
Mage::app();

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

$setup = Mage::getResourceModel('catalog/setup', 'core_setup');
try {
    $setup->startSetup();
    $setup->removeAttribute('catalog_product', $attr);
    $setup->endSetup();
    echo $attr . " attribute is removed";
} catch (Mage_Core_Exception $e) {
    print_r($e->getMessage());
}
3
  • 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
1

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',
    'camera_type','coater','color','colors','cost','decor_type','ebizmarts_mark_visited','electronic_type','featured','fit','format','frame_style','gender','gendered','genre','homeware_style',
    'home_decor_type','impressions','is_sold','jewelry_type','length','lens_type','luggage_size','luggage_style','luggage_travel_style','make','manufacturer','material','model','necklace_length',
    'occasion','perfector','sample_item_1','sheet_size','shoe_size','shoe_type','size','sleeve_length','style',);

        $entityType = 'catalog_product';

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

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

                if($attributeId)){
                    Mage::getModel('catalog/product_attribute_api')->remove($attributeId);          
                }
        }
2
  • 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
1

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

www.yourwebsitename.com/index.php/admin/catalog_product_attribute/edit/attribute_id/533/

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;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$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.

www.yourwebsitename.com/delete_my_attribute.php

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

0

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:

<?php
include 'app/Mage.php';
Mage::app();
$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 :

<?php
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');

$installer->startSetup();

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

$installer->endSetup();
?>

in

app\code\local\YourModuleNamespace\YourModuleName\sql\yourmodulenamespace_yourmodulename_setup\mysql4-upgrade-yourmoduleversion.php

example :

app\code\local\Andi\Productattributes\sql\andhi_productattributes_setup\mysql4-upgrade-1.1.4-1.1.5.php

Note : check your module xml for your module version

in this example :

app\code\local\Andi\Productattributes\etc\config.xml

Tested and working well in Magento 1.9.2.2 and Magento 1.9.3.8

0

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:

http://example.com/removeAttributes.php?entity_type=catalog_category&attributes=category_name,is_active

Code:

<?php
error_reporting(E_ALL | E_STRICT);

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

try{
    Mage::getSingleton('core/session', array('name'=>'adminhtml'));
    if(!Mage::getSingleton('admin/session')->isLoggedIn()){
        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);

        if($attribute->getId()){
            $attribute->delete();
            $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.