WordPress Code Snippet

Delete Thumbnail Image from Media Library when deleting Post

This code snippet automatically deletes the Thumbnail Image from the WordPress Media Library when you delete a post. Deleting of the image takes place when finally deleting a post from the Trash. As long as a post remains in Trash the image is kept, so you can restore the post anytime. The code first checks if there is a thumbnail assigned to the post. If so it checks, if the image is used as a Thumbnail for another post. In that case the image is not deleted. Otherwise the code checks if the image is used somewhere inside the content of another post. In that case the image also is not deleted. If the image is used nowhere else, it is automatically deleted to keep your Media Library free from garbage.

Share this page: