Magento: Shell script for cleaning associated product attribute options

During the last 2 months I have been trying different strategies for product imports. Also there are very often change requests about what options we should present for the product attributes. It’s  constant fun, because we have 2 data sources – PIM and ERP, but of course the client  has the final word.

From technical point of view it’s important to mention that we are using custom source tables for almost every product attribute. And we have a highly customized Magento store.

I ended up in a situation that I had to delete all the associated product attribute options. As those might lead to some problems in the future. This is because of the specific Magento setup and the custom source tables that we have. In our project if I truncate any of the custom source tables, the options ids in in any of catalog_product_entity_* tables will not be deleted automatically. But I had to rebuild the data of some of the custom tables with completely different values than what we had previously.

Basically I wrote a simple shell script. By given product attribute code deletes it’s all associated options / values from catalog_product_entity_* tables.

Below I am going to share the product attribute script. Before you use it you might check these usage instructions:

To use the script, you should put it in the shell folder of your Magento installation.

The product attribute script:

Example usage:

The output:

Room for improvement:

There are suggestions to make the product attribute script more powerful. For example in the future it can get extra parameters. For example to determine which product types you may want to remove the associated product attribute options. So far I don’t need such a functionality. But if you need something extra feel free to modify the script and to use it for your own benefits.

Final words:

Many thanks to Benjamin Alsleben, who made some changes in the script and made the output to look better.

Proof reading by Ville Köykkä and Zsombor Nagy.
Thanks!