Find unused images in WordPress

How to find unused images in WordPress. Here are the three most popular ways: Check unused images through the media library, using a plugin, or writing custom SQL queries.

I built and maintained many active WordPress websites. Some with millions of pages and tens of millions of page views. Others have 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 files 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 add up to hundreds of megabytes, affecting your site’s performance and storage.

In this comprehensive guide, I will explore how to find unused images in WordPress and offer practical strategies for decluttering your media library. I have used all of these over the years to clean up the media libraries I was responsible for.


💡 Quick Solution: Find unused images automatically with Image Source Control’s Deep Check.

Summary

To find unused images in WordPress, you can either use a Plugin like Image Source Control or write custom SQL queries. Manually checking the Media Library might find images that are still used in options and post meta data, like page builders create them.

1. Using Plugins:

  • Image Source Control thoroughly scans your site’s content and database for image usage and provides a detailed report. It will find any unused images.

2. SQL Queries (for advanced users):

  • If you are comfortable with SQL, you can write queries to directly access the WordPress database and identify unused images. This method offers the most precision but requires technical expertise. See examples below.

Important:

  • Ideally, back up your website before deleting any images.

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 automatically creates smaller copies of each uploaded image by default, a single image, let’s say with 3 megabytes, can lead to 5 images that sum up to 6 megabytes in disk space.

The number of unused images and the disk space it reserves showing in the WordPress dashboard.
The number of unused images and their disk space, as calculated by Image Source Control.

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.

While storage limits are rarely an issue on modern hosting, unused images significantly increase backup size and time. On one of my sites with 800 unused images (totaling 2GB), backups took 45 minutes instead of 12 minutes. After cleanup, backup time dropped by 73%.

In addition to the backup time, a large number of unused image files significantly increase backup sizes. Other than text-based files, images cannot be compressed much. This is an area where I have indeed encountered limitations in the past, and cleaning up unused photos has 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.

Image Library in List view with the Uploaded To column highlighted.

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 find unused images in WordPress

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.

Steps to remove unused images in a WordPress plugin.
Find all unused images listed on a dedicated page.

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. Some themes and plugins store image-related information here, including:

  • WordPress core (favicon, custom header)
  • WooCommerce (product images, galleries, variations)
  • Page builders (Elementor, Divi, Beaver Builder)
  • Theme customizer settings

Use the bulk options to delete unused images now.

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.

Finding unused images on all pages using the Indexer feature.
The Indexer automatically scans your site to find unused images on all pages.

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 (
    SELECT 1
    FROM wp_posts p2
    WHERE p2.post_status = 'publish'
    AND p2.post_content LIKE CONCAT('%', p1.guid, '%')
);Code language: JavaScript (javascript)

This query does the following:

  1. It selects all posts from wp_posts where the post_type is ‘attachment,’ and the MIME type indicates an image.
  2. It then checks if the guid of these attachments exists in the post_content of 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.

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);

Replace 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 WordPress media library cleanup as a regular part of your website maintenance routine. I have reserved time for this in every quarter.

FAQ

How do I find unused images in WordPress?

The most reliable way is to use a plugin like Image Source Control that performs a Deep Check of your entire site. It scans post content, meta fields, options, page builders, and WooCommerce to identify truly unused images. Alternatively, you can use SQL queries if you’re an experienced developer.

What are unused images in WordPress?

Unused images are files in your WordPress media library that aren’t displayed anywhere on your website. This includes images not used in posts, pages, widgets, theme settings, or page builders. Note that “unattached” images (shown in Media Library filter) are NOT the same as unused images.

Can I find unused images without a plugin?

Yes, but it’s extremely time-consuming and error-prone. You’d need to manually check the “Uploaded to” column in the Media Library list view, then verify each image isn’t used in widgets, theme settings, or page builders. For a site with 100+ images, this could take hours or days.

How do I find orphaned images in WordPress?

Orphaned images are images that exist in your media library but aren’t referenced anywhere. Use Image Source Control’s Deep Check feature to scan your entire site, including post content, meta fields, options, and page builders. The plugin will list all images it couldn’t find anywhere.

Does Image Source Control work with WooCommerce?

Yes, the Deep Check feature scans WooCommerce product images, galleries, and variation images. This ensures that product images are never incorrectly marked as “unused” even if they’re only used in WooCommerce and not in regular posts or pages.

How long does it take to find unused images?

With Image Source Control, the Deep Check typically takes 1-5 minutes for most WordPress sites. Larger sites with thousands of images may take longer. Compared to using the plugin, manual checking can take hours or days, while SQL queries require technical expertise and careful verification.

Portrait of Thomas Maier, founder and CEO of Image Source Control

Questions? Feedback? How can I help?

Reach out directly via the contact form.