28

I want to remove unused EAV attributes directly from the database before I move my store live. Attributes can be found in eav_attribute table, can I delete attributes from this table? Is it safe? Or do I also need to edit other EAV tables?

3 Answers 3

39

See Mage_Eav_Model_Entity_Setup::removeAttribute(). It takes two arguments - the first is the entity code, and the second is the attribute code.

Edit - to run from a non-installation scope:

<?php
include 'app/Mage.php';
Mage::app();
$setup = Mage::getResourceModel('catalog/setup','catalog_setup');
$setup->removeAttribute('catalog_product','attr_code');
4
  • 1
    Thnk you. I understand how to use it in a setup script of a module: $installer->removeAttribute('catalog_product', 'my_attribute1'); or $installer->removeAttribute('catalog_category', 'my_attribute2'); But is there a way to use this code in a separate file? I'd like to place the file in the root folder (where Magento's index.php), so that it would be possible to call the script in web browser: example.com/delete_attributes.php/. Can you point me in the right direction?
    – zitix
    Jul 7, 2013 at 9:31
  • Updated my response.
    – benmarks
    Jul 7, 2013 at 15:30
  • What code changes i need to do, to delete a custom customer attribute? Jun 9, 2017 at 8:14
  • 1
    @shasikanth - it's really a separate question, but swap catalog/setup with customer/setup, catalog_setup with customer_setup, and catalog_product with customer.
    – benmarks
    Jun 13, 2017 at 20:57
12

The first rule of Magento is: Never edit the database directly.
I admit that I broke this rule on numerous occasions, so...
you can delete the attributes from eav_attribute, the constraints with ON DELETE CASCADE should cleanup the rest of the tables.
But I still think you should take the clean way:

$attributeId = 55;
Mage::getModel('catalog/resource_eav_attribute')->load($attributeId)->delete();

It shouldn't take long and you will feel at peace with yourself because you didn't break the rules.
It's not important what method you choose, but backing up your DB is, in both cases.

2
  • 3
    I always try to avoid editing database, but it is much faster to do it this way :) and this time it is only for testing (not for production site). So is this query enough to get rid of the attribute, or do I need to do something more: DELETE FROM eav_attribute WHERE eav_attribute.attribute_code = "my_attribute";
    – zitix
    Jul 7, 2013 at 9:11
  • 1
    This should be eoungh. Just make sure there aren't 2 attributes with the same code, for different entities.
    – Marius
    Jul 7, 2013 at 11:10
5

DELETE FROM eav_attribute WHERE eav_attribute.attribute_code = "some_attr_code";

is a working solution, I used it many times.

Especially if you remove extension and Magento still want to call unexisting attribute model

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.