I built and maintained many active WordPress websites. Some with millions of pages and tens of millions of page views. Others with constantly updated content and tutorials. Something they all have in common is that, at one point, no single person has an overview of everything, and unused resources start piling up.
One often overlooked aspect is the management of media files, particularly images. While an unused text snippet is just a few bytes of data, unused images can quickly sum up to hundreds of megabytes, affecting your site’s performance and storage.
In this comprehensive guide, I will delve into how to find unused images in WordPress, offering practical strategies to declutter your media library. I have used all of these over the years to clean up the media libraries I was in charge of.
Why to remove Unused Images?
Before I dive into the solutions, let’s understand why this issue demands your attention. Unused images in WordPress can be a silent killer for your, your colleagues’, and your server’s efficiency.
The technical part is easy to understand and measure. Since WordPress, by default, automatically creates smaller copies of each uploaded image, a single image, let’s say with 3 megabytes, can lead to 5 images that sum up to 6 megabytes in disk space.
One strategy I am using to reduce this is by minifying files using the free online tool TinyPNG before uploading or a paid service like Shortpixel that reduces the file size of images in the media library. Reduced image file sizes also come with the benefit of fast load times for your users.
Even on the smallest hosting packages, none of my projects ever had an issue with file or storage limits due to too many images in the media library. Though these scenarios could exist. What I find more annoying about a large number of unused image files is that they significantly increase backups in size and time. Because, other than text-based files, images cannot be compressed a lot. This is something where I did indeed run into limitations in the past, and cleaning up unused photos helped a lot.
While file sizes only occasionally bother me, unused images occupy much of my mental space. When writing tutorials, I try to reuse existing images uploaded to other posts. I mostly find them through the media library search. When no one cleaned up the media library for some time, I often had to go through multiple old screenshots and set up our software to see if they were still up to date. I know that my colleagues have the same problem.
Now, let’s see which methods I have tried in the past to find unused images in WordPress and clean up the media library for more efficiency.
List old images in the media library
Of course, the first method to find unused images is the WordPress media library. However, this method is highly time-consuming and prone to error mostly because WordPress core does not have a function to track where an image is used.
You can look for the “Attached to” information in the media library’s list view, but it only shows the post the image was uploaded to. This information doesn’t change when an image is removed from that post, nor does it show other posts to which the image was added.
Below, you see how to enable the list view at the top left and where to find the “Uploaded to” section.
Conclusion: The Uploaded-To information is a decent start to find where an image is used, but not where it isn’t.
A Plugin to identify unused images
After being frustrated with the media library not showing unused images and – for years – spending hours fiddling in the database to do that, I noticed that the Image Source Control plugin I originally built to show image captions could be tweaked to help with this.
The result is a dedicated “Unused Images” overview in the WordPress backend that shows me all the images the plugin couldn’t find anywhere on the site.
To manage image captions and copyright holder information, Image Source Control already comes with an internal index of where an image is used. One does not need to manually attach images to specific posts as the WordPress core does. ISC also finds pictures outside of the content.
The additional Deep Check option looks for images in post meta information or options. This is where some themes and plugins store image-related information. Even WordPress core does that for the favicon.
Use the bulk options to now delete unused images.
Image Source Control is the solution I am using now to remove unused images from WordPress on all of my projects. Give it a try, and let me know how I can further improve it for your workflows.
SQL Queries for Advanced Users
For those well-versed in SQL and database management, using SQL queries can be the most precise and efficient way to find unused images in WordPress.
Using custom SQL queries gives me unparalleled control over the process, allowing me to see where an image is used. However, it’s worth noting that this approach is risky; a single mistake could corrupt the database.
So before you write any SQL, create a backup of your database. Or do as I do, if your hosting allows it, and make a copy of the database in PHPMyAdmin to run all the queries in that copy before applying them to the production database.
Finding unused images with SQL
An accurate approach to finding unused media would be to look for attachments that are not referenced in the
post_content column of the
wp_posts table. Here’s the SQL query you can use to do that:
SELECT p1.ID, p1.post_title
FROM wp_posts p1
WHERE p1.post_type = 'attachment'
AND p1.post_mime_type LIKE 'image%'
AND NOT EXISTS (
FROM wp_posts p2
WHERE p2.post_status = 'publish'
AND p2.post_content LIKE CONCAT('%', p1.guid, '%')
This query does the following:
- It selects all posts from
post_typeis ‘attachment,’ and the MIME type indicates an image.
- It then checks if the
guidof these attachments exists in the
post_contentof any published posts.
You must adjust this query to check for image usage in widgets, options, or other custom fields. Each use case would need another custom SQL query. If you don’t know how to write them or lack the funds to pay a developer to spend hours on this, check out Image Source Control as a reliable, non-technical solution.
Run a full-text search
I mostly used the full-text search in PHPMyAdmin to find where an individual image is used. This helps find images anywhere, not just in post content.
This method needs a decent understanding of the WordPress database structure so that you know how to handle found entries. Remembering how WordPress handles image files and various suffixes would be best.
Please see my in-depth tutorial on how to find where an image is used in WordPress for more details.
If you are not an experienced developer, you might want to check out Image Source Control as a reliable way to check for unused images.
Delete Unused Images
After verification, you can either delete unused images manually from the WordPress dashboard or run another SQL query to remove them from the database. I would always advise using the WordPress backend to remove unused images since it also deletes the files on your server and additional dependencies.
However, exercise extreme caution when deleting directly from the database.
DELETE FROM wp_posts WHERE ID IN (list_of_unused_image_ids);
DELETE FROM wp_postmeta WHERE post_id IN (list_of_unused_image_ids);
list_of_unused_image_ids with the comma-separated IDs of the images you’ve confirmed are unused.
Conclusion: Time to Declutter Your WordPress Media Library
Unused images can silently degrade your WordPress website’s performance and user experience. They did that on my sites for years.
Whether you opt for manual inspection, use Image Source Control, or go the SQL route, I’d suggest optimizing your media library as a regular part of your website maintenance routine. I have reserved time for this in every quarter.