In this tutorial, I will show you a simple standalone script to remove programmatically an Attribute in Magento 2. It applies either for Customer Attributes, Product Attributes, and each Attribute Entity.

There some scenarios where you might have an extension that added, for instance, a Customer Attribute but you decided to remove that module but the customer attribute will remain there. In order to clean it up, you can just write a script similar to this one, place it on the root directory and execute it via command line.

  1. Create a remove-attribute.php file on your project root
  2. Put the following content on it and modify the [YOUR_ATTRIBUTE_CODE] parameter with your attribute code.

  3. Now you just need to run php -f test.php within your project root directory

And that should do the trick. You can check on your eav_attribute table and double check if the attribute was removed properly. Make sure to reindex and flush your cache after this process.