Woocommerce when deleting a product, automatically also delete the product gallery and product image

I was trying the following:

when a product page gets deleted (permanently), it should also delete the:
product gallery and product image which are attached to the post.

Read More

I tried the solution i found on:
https://wordpress.stackexchange.com/questions/109793/delete-associated-media-upon-page-deletion

But it didn’t work out for me.

Related posts

Leave a Reply

6 comments

  1. Although this is an old question, I will leave it here in case someone will be looking for it in the future.

    Since you want to delete product images, you may try these neat and handy woocommerce functions to get attachments ids.

    Something like this:

    add_action( 'before_delete_post', 'delete_product_images', 10, 1 );
    
    function delete_product_images( $post_id )
    {
        $product = wc_get_product( $post_id );
    
        if ( !$product ) {
            return;
        }
    
        $featured_image_id = $product->get_image_id();
        $image_galleries_id = $product->get_gallery_image_ids();
    
        if( !empty( $featured_image_id ) ) {
            wp_delete_post( $featured_image_id );
        }
    
        if( !empty( $image_galleries_id ) ) {
            foreach( $image_galleries_id as $single_image_id ) {
                wp_delete_post( $single_image_id );
            }
        }
    }
    

    Works fine for me.

  2. add_action("before_delete_post","wdm_delete_post_images",10,1);
    
    function wdm_delete_post_images($post_id)
    {
     global $wpdb;
              $args = array(
                    'post_parent' => $post_id,
                    'post_type'   => 'attachment', 
                    'numberposts' => -1,
                    'post_status' => 'any' 
            ); 
            $childrens = get_children( $args);
            if($childrens):
                foreach($childrens as $attachment):   
                 wp_delete_attachment( $attachment->ID, true ); 
                 $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = ".$attachment->ID);
                 wp_delete_post($attachment->ID,true ); 
                endforeach;
    
            endif;
    }
    
  3. you can try the following:

    add_action('after_delete_post','wdm_delete_post_images',10,1);
    
    function wdm_delete_post_images($post_id)
    {
       $featured_image_id = get_post_meta($post_id,'_thumbnail_id',true);
    
       $image_galleries_id = get_post_meta($post_id,'_product_image_gallery',true);
    
       if(!empty($featured_image_id))
       {
         wp_delete_post($featured_image_id);
       }
    
      if(!empty($image_galleries_id))
      {
        $image_galleries_array = split(',',$image_galleries_id);
    
        foreach($image_galleries_array as $single_image_id)
        {
            wp_delete_post($single_image_id);
        }
      }
    }
    

    This will delete images when product is permanently deleted.

  4. Step 1: Login to Your PhpMyAdmin | If you have more than one database, make sure you click on the correct database. If you’re not sure because you have more than one WordPress installation on the same domain, open a table and look for “wp_options”, you’ll be able to see the URL of the website you’re making changes to.

    Step 2: Run the SQL Statement ==> in WP_POST

    If you are on the correct database, click on the SQL tab on the top right. It might be empty or have an SQL statement there from a previous command.

    DELETE relations.*, taxes.*, terms.*
    FROM wp_term_relationships AS relations
    INNER JOIN wp_term_taxonomy AS taxes
    ON relations.term_taxonomy_id=taxes.term_taxonomy_id
    INNER JOIN wp_terms AS terms
    ON taxes.term_id=terms.term_id
    WHERE object_id IN (SELECT ID FROM wp_posts WHERE post_type='product');
    
    DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = 'product');
    DELETE FROM wp_posts WHERE post_type = 'product';
    
  5. Try it one time not need any more code for delete product.
    You can add this code in one new file and place in root directory of wordpress.

    include('wp-load.php');
        function delete_post($sku)
        {
            global $wpdb;
            $result_post = $wpdb->get_results ( "SELECT `ID` FROM `wp_posts` WHERE `post_name` LIKE CONCAT('%',".$sku.", '%')" );
                for($q=0;$q<count($result_post);$q++)
                {
                    wp_delete_post($result_post[$q]->ID,true); // wp_delete_post full product delete with its all images
                }
        }
    
        delete_post($sku); 
    
  6. i used this code and it works. But I’m trying to add more code to create a function that when I delete a product definitively, this in addition to being deleted from the wordpress “media” folder, is deleted from another folder in my ftp, where the same images are present with the same names. so I simply need to know the name of the file, delete it from the “media” folder and then delete it from another folder where there is a copy of that file. How can I do?

    add_action( 'before_delete_post', 'delete_product_images', 10, 1 );
    
    function delete_product_images( $post_id )
    {
    $product = wc_get_product( $post_id );
    
    if ( !$product ) {
        return;
    }
    
    $featured_image_id = $product->get_image_id();
    $image_galleries_id = $product->get_gallery_image_ids();
    
    if( !empty( $featured_image_id ) ) {
        wp_delete_post( $featured_image_id );
    }
    
    if( !empty( $image_galleries_id ) ) {
        foreach( $image_galleries_id as $single_image_id ) {
            wp_delete_post( $single_image_id );
        }
    }
    

    }
    }