• I’d like to remove gallery images only (not featured image) from sold products on my woocommerce site in bulk because my server storage limits are being reached. I have nearly 2000 out of stock products, so doing this manually is not an option. I do not want to delete the products or featured image, only the gallery images.

    I couldn’t find a plugin that would do this, so just to get something working quickly I tried to create an sql query that would remove the product ID from wp_posts.post_parent for just the product gallery attachments but my query isn’t working. The idea is if the query can unattach the images, then I can go into the media library and delete all unattached images in bulk which should also delete the physical file from the server.

    Here is my attempt, hopefully someone can look at this and see the problem:

    UPDATE
    wp_posts
    inner join
    wp_postmeta on find_in_set(wp_posts.ID, wp_postmeta.meta_value)
    SET
    wp_posts.post_parent = ”
    WHERE
    wp_postmeta.meta_value = ‘outofstock’
    AND
    wp_postmeta.meta_value = wp_posts.post_parent
    AND
    wp_posts.post_type = ‘attachment’
    AND
    wp_posts.comment_status = ‘open’;

    Thanks!

  • The topic ‘Remove gallery images from out of stock products sql query?’ is closed to new replies.