1

I'm trying to remove product image (one at a time) using WooCommerce API but I'm facing a strange problem.

I can remove all associated image at once by sending an empty array in images: { images:[] }

But I can't find a way to remove one image at a time.

5
  • Do you want to delete the images, or just remove them from the product? Sending an empty array probably only does the latter. Regardless, WooCommerce images are WordPress attachments, so you could delete them using the media endpoint, if you have their ID: developer.wordpress.org/rest-api/reference/media Commented Jun 16, 2019 at 10:27
  • I want to remove image from a product. Now, sending empty array removes all images, but I want to remove one specific image from the product while keeping others as they were. I have figured out one hack: Let's assume I want to delete this image: "id":123. I sent all other images in an array in the following manner: { "images": [ { "id": 209101 }, { "id": 209102 }, { "id": 209103 } ] } This is updating the array and removing the image "id":123, while keeping other images as they were. But I am looking for a cleaner solution. :)
    – Qumber
    Commented Jun 17, 2019 at 11:29
  • 3
    That’s not a hack. That seems like a perfectly reasonable approach. Commented Jun 17, 2019 at 11:32
  • Is there not a way where I can simply remove the image by calling it using its id or something rather than the other way around? Thanks, by the way. 😊
    – Qumber
    Commented Jun 17, 2019 at 11:57
  • The problem with the above is that while it will remove the image, it won't delete it from the media library. So one would need separate code for that.
    – DarkNeuron
    Commented Oct 26, 2022 at 8:38

1 Answer 1

1

The image array you are sending includes all images you want attached to the product. If you want to remove one image, you need to send all original image id's minus the one you want to remove,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.