Deleting Sample Products in Magento 2 While Keeping Categories and Luma Layout
As a senior developer working extensively with Magento 2, I often receive queries from merchants regarding the deletion of sample data without affecting core elements like categories or layouts. In this post, we’ll walk through an efficient way to achieve this using Magento’s built-in tools.
Understanding the Sample Data Structure in Magento 2
Magento 2 ships with a set of default samples that include products, customers, orders, invoices, and more. These are designed to provide a starting point for new installations but can become cluttered over time.
The primary entities you’ll want to target for deletion are:
catalog_product_entitysales_ordercustomer_entity
However, the categories and layout structure reside in different tables:
- Categories:
eav_attribute,catalog_category_entity, etc. - Luma Layout: Various XML files under
app/design/frontend/Magento/luma/etc/theme.xmland other theme-related directories
Deleting Sample Products Without Affecting Core Data
To delete the sample products while preserving categories and layout, follow these steps:
-
Backup Your Database: Always backup your database before performing significant operations to avoid data loss.
You can use MySQL dump:
mysqldump -u [username] -p[password] --no-create-info --complete-insert --default-character-set=utf8 [database_name] > backup.sql -
Identify Sample Product IDs: Use a SQL query to find the product IDs of the sample products.
SELECT entity_id FROM catalog_product_entity WHERE attribute_set_id = 4; -
Delete Sample Products in Batches: Magento recommends deleting data in batches to prevent performance degradation.
You can write a script using PHP and Magento’s API to delete products gradually:
require_once 'app/bootstrap.php'; $productCollectionFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class); $collection = $productCollectionFactory->create(); $collection->addAttributeToFilter('attribute_set_id', 4); // Filter for the sample set foreach ($collection as $product) { try { $product->delete(); echo "Deleted product ID: " . $product->getId() . PHP_EOL; } catch (Exception $e) { echo "Error deleting product " . $product->getSku() . ": " . $e->getMessage() . PHP_EOL; } } -
Clear Cache: After the deletion, clear your Magento cache to ensure the changes take effect.
Maintaining Categories and Luma Layout
The categories are not affected by product deletions as they exist independently in their own tables. The Luma layout remains intact since it’s stored in XML files rather than the database.
However, if you want to manage your themes or layouts more effectively:
-
Switch to a Custom Theme: Consider creating a custom theme by copying and modifying the
lumatheme. This allows you to have full control over the layout without affecting the core files.You can learn more about creating custom themes in our comprehensive guide on theme development at Laravel Company.
Conclusion
Deleting sample data in Magento 2 is a straightforward process when approached systematically. By identifying the relevant entities and using Magento’s built-in tools or APIs, you can efficiently remove unwanted samples without compromising your store’s core structure.
Remember, always backup your database before performing significant operations to ensure you can revert changes if needed. Keeping your themes under version control also helps maintain a clean and manageable codebase.
If you’re looking for more advanced Magento 2 development insights, feel free to explore our Magento 2 tutorials at Laravel Company.