Blog

Wordpress. Woocommerce. Remove Product Attachments where Image Files are Missing

Woocommerce is widely used shopping cart. However, sometimes when things go wrong it will not provide easy solution.

One such instance is if you delete your uploads folder. This will leave your products, but the image files will be missing. This will leave the products showing on your website as these are in the database, but the images will show as broken, as still the attachments will be listed in the database.

There is no easy way to remove these attachments, unless you go and manually delete these for each product. This can be fine for small amount of products, but if you have many roducts in my case over 20,000 its not a viable option.

Below is a script, which will check if the files exist, and if they do not will remove the attachments.

https://gist.github.com/lesichkovm/6514f0c5d99a76a8b10da1efcee8c2e7

Happy Wordpressing!!