Last Updated: October 29, 2016
·
751
· sivaschenko

Magento 2.Remove attributes that are not in attribute set

DELETE FROM catalog_product_entity_int atr
WHERE atr.attribute_id NOT IN (
  SELECT attribute_id
  FROM eav_entity_attribute a
  WHERE a.attribute_set_id = (
      SELECT attribute_set_id
      FROM catalog_product_entity e
      WHERE e.entity_id = atr.entity_id
   )
);

Repeat this query for all attribute type tables.
All attribute type queries can be selected using following query:

SELECT concat('catalog_product_entity_', backend_type)
FROM eav_attribute
WHERE backend_type != 'static'
GROUP BY backend_type;

Can anybody advice how to perform this foreach in one query?