Prompt Detail:
To delete store-specific media images and only keep the global (all store views) images in Magento 2 using SQL, you can follow these steps:
- Backup your database before performing any deletion operations.
- Identify the specific store ID for which you want to delete the media images. You can find the store ID in the
store
table, typically referenced by thecode
orstore_id
column. - Execute the following SQL queries:
sql-- Step 1: Delete store-specific image records from the 'catalog_product_entity_media_gallery_value' table
DELETE FROM catalog_product_entity_media_gallery_value
WHERE value_id IN (
SELECT value_id
FROM catalog_product_entity_media_gallery
WHERE value_id NOT IN (
SELECT value_id
FROM catalog_product_entity_media_gallery_value_to_entity
)
);
-- Step 2: Delete store-specific image records from the 'catalog_product_entity_media_gallery' table
DELETE FROM catalog_product_entity_media_gallery
WHERE value_id NOT IN (
SELECT value_id
FROM catalog_product_entity_media_gallery_value
);
-- Step 3: Clean up orphaned image records from the 'catalog_product_entity_varchar' table
DELETE FROM catalog_product_entity_varchar
WHERE attribute_id IN (
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'media_gallery'
)
AND row_id NOT IN (
SELECT value_id
FROM catalog_product_entity_media_gallery_value
);
-- Step 4: Remove store-specific image files from the 'pub/media/catalog/product' directory
-- Manually delete the image files associated with the specific store from the 'pub/media/catalog/product' directory using your preferred file management method.
These SQL queries perform the following actions:
- Step 1: Deletes store-specific image records from the
catalog_product_entity_media_gallery_value
table for the specific store. - Step 2: Deletes store-specific image records from the
catalog_product_entity_media_gallery
table for the specific store. - Step 3: Cleans up orphaned image records from the
catalog_product_entity_varchar
table related to themedia_gallery
attribute. - Step 4: Manually removes the store-specific image files from the
pub/media/catalog/product
directory using your preferred file management method.
Please exercise caution when executing these SQL queries, and ensure you have a backup of your data before proceeding. It is always recommended to perform such operations in a test environment first.
Add a comment