I’ve been running a WordPress blog for a long time. Is there a simple and reliable way to delete the unused images I’ve uploaded but then never actually included in a post?
You would like to think that there’s a simple checkbox on every image entry in the WordPress database that let it track whether the image, audio, video or similar had ever been added to a post or page. But there isn’t, though it seems like there is with “unattached”. Turns out that the way WordPress actually keeps track of what is and isn’t used is a bit Byzantine – and doesn’t work reliably at all.
Over on the WordPress forums, one helpful commentator explains unattached this way: “Media is only attached to a page or post when it is uploaded from the edit screen for that particular post or page. Adding to the media library from the Media section of the admin won’t attach it to the page.” Got it?
Yeah, me neither. In fact, it might require a bit of detective work on your part. One thing I do reliably is that when I upload an image it doesn’t get assigned a default alt attribute, but 100% of the time when I include an image in a blog post or page, I do give it an alt attribute. That gives me a pretty strong clue about what has or hasn’t been attached or embedded.
But let’s start at the top. Within WordPress, your Media Library will probably look like this:
The selector along the top should be useful, right? I mean, there are all these choices:
Choose “unattached” and you should get all the uploaded but unused images, right? Ah, if only.
See that very first image with the beige boxes against the black background? A click and you can see a lot more details and information about an image that WordPress believes is unattached:
WordPress thinks it’s unattached, but I’ve specified an Alt Text. Meaning, this particular image is actually in use. My naming scheme gives me another clue too: “-fp” means it’s a featured photo or image for an article. As you can see, WordPress is not being helpful here.
On the other hand, here’s another image that wasn’t flagged as unattached but, by my “did you add an alt attribute” test, isn’t included in any of my posts or pages:
I do recall uploading this and I know that I didn’t use it in the post. So it’s safe to delete! That’s done by clicking on the red text link on the lower right “Delete Permanently“. A click and there’s a warning box…
Yup, I can do this! A click on “OK” deletes the picture and keeps my WordPress database slim and uncluttered by unused image files. Now if only the “unattached” feature worked…
Pro Tip: We have lots of WordPress insider tips to help your site run smoothly! Check ’em out.