Deleting Unused Images in WordPress: A Guide to Cleaner, Faster Websites

I am a fan of regular reviews and decluttering of my digital and real-life projects. It benefits my mental health and saves me time and money in the future. During these reviews, I check up on all my projects, domains, and subscribed services and clean up unnecessary ones. Then, I go into individual websites and clean them up, too. One of these steps is deleting unused images. The Importance of Deleting Unused Images I’ve found that deleting unused images from my WordPress sites, while not directly influencing the frontend speed, frees up time and mental space while saving money. Here’s a more accurate breakdown of the advantages: Enhanced Site Management : By removing clutter from my media library, I streamline the backend of my website, making it quicker and easier for me to find and use the images I am looking for. Lower Storage Space Costs : As I recently experienced painfully with one of my smaller sites, some hosts have limited storage space. With each image taking up more storage space than the original version, you might need to upgrade sooner rather than later — or delete unused images, as I did in this case. Quicker Backups : The size of your website backups can affect how quickly and smoothly this essential task finishes. Larger sites take longer to back up and restore. By keeping my digital footprint lean, I ensure backups are as efficient as possible. Reduced Risks Of Outdated Files : I write a lot of tutorials about using images in WordPress and can often reuse images in multiple posts. I have to ensure that the photos in my media library are up to date. Faster Website Migration : If you ever need to move your site, having a leaner media library can make the migration process faster and less prone to errors. It’s like moving houses; the less excess baggage you have, the easier the move. Since I manage dozens of sites, this happens more often than you’d think. Why each image matters I noticed in many conversations with my clients that especially the less technical ones are unaware that WordPress creates multiple versions of each image they upload. Depending on the image file itself, your theme, and WordPress settings, there can be half a dozen or more image files created by WordPress on your server, while you only see a single one in the media library. WordPress displays these resized files on smaller screens. This is a great way to speed up your site, but it increases the number of files and storage needed on your server. An uploaded photo can easily have a file size of 3 MB, and with all resized versions created, the total space can take more than 6 MB on your server. Finding Unused Images Before deleting unused images, I clean up on a higher level. This includes removing unused plugins as well as articles and pages. The latter happens very rarely, though. What many publishers, and even WordPress developers, don’t know: The “Attached to” section in the backend only tells you which post an image was uploaded to, but neither if it is still in use there nor where else you might have added it. I used to write custom SQL queries to identify unused images directly in the database. Here is such a query you could test on your end: 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, ‘%’)
);

You could run this query every few months, then go through the URLs and do a full database search for them to be sure that the images are not used anywhere else. What do I mean by anywhere else? There are a lot of places where images are used outside of the content: Logo and Favicons Featured images of posts Page Builders use them for illustration and background images Advanced Custom Fields lets you upload images as metadata Luckily, there is a more straightforward way to do this using the Image Source Control plugin. Deleting Unused Images Even though, as a WordPress developer, I know how to find and delete unused images in the database, it is still a time-consuming and tedious task. So, nowadays, I am deleting unused images with Image Source Control. As you may already know, I developed the plugin to manage author attributions for images. With that much insight into where images are used and displayed, extending the plugin’s features to Deleting Unused Images was easy. Media > Unused Images lists all possibly unused images. I usually use the bulk option to run a deep check on all listed images and then look at all those not associated with any posts. This includes images mentioned in some options or post metadata. Finally, I check the unused images and bulk-delete them to clean up the space. Conclusion I was surprised by how much data clutter I could free up and how accomplished that felt. The gratitude of my publisher colleagues, who realized how easy it suddenly was to find images again in a well-organized media library, came as a welcomed bonus. Depending on the publishing activity, I now delete unused images during my maintenance reviews every couple of months and right before creating staging sites.

This content was originally published here.