As you continuously publish new posts with images, your media library will also grow in size, until one day, you’ll start running out of disk space. Of course, you must do something about it or else you won’t be able to publish new posts. However, before you decide to upgrade your web hosting service or buy additional disk space, you might want to try deleting unused media files first and you might be surprised how many MB or even GB (for very large sites) you’ll be able to save.
By default, WordPress creates multiple resized copies of your images. These copies are used to display thumbnails. These duplicated copies are not shown in your media library. You need to go to your wp-content/uploads folder on your server to be able to see them.
Typically, the larger an image’s dimensions are, the more duplicates it will have. High-resolution images actually take up more disk space than you think and leaving dozens of unused high-resolution images in your media library.
Removing unused media files in WordPress is easy. All you have to do is to select the “Unattached” category from the drop-down in the menu bar found in the media library:
It will now display all “unattached” media files; that is, it will display all media files that aren’t being used anywhere on your site. You can delete them all, but it’s recommended that you create a backup of your wp-content/uploads directory first so you can restore them in case something goes wrong.
If you prefer to use a plugin, there is one called Media Cleaner that works very well. It can help you find which of your media files are not being used and it has an option that allows you to “delete” your unused media files by moving them into a temporary directory. This is useful if you want to “delete” an unused media file first and check if something on your site suddenly broke. If there is, you can easily restore your “deleted” media file.
How to Enable Media Cleaner’s “Trash” Feature
If you want to make use of this invaluable feature, you will need to edit your wp-config.php file. Create a backup of this file first before proceeding so you can easily restore your site in its original state if things go south. Once you have the backup, open your wp-config.php file and add this code:
define( ‘MEDIA_TRASH’, true );
According to the plugin, you should put the code as high as possible inside the file, like in the image below:
Save the file and you’re done!
Before starting to use the plugin, make sure that you enable the option to scan posts, post meta, galleries, and widgets in Meow Apps > Media Cleaner.
Once you enable them, click the Save Changes button at the bottom and you’re ready to go.
What About Manually Embedded and/or Inserted Media Files?
If you use a remote publishing tool or if you embedded your media files manually using HTML code, then the Media Library will still display the media file as “unattached” and sometimes, even the “Media Cleaner” plugin too (most of the time it does a good job of accurately knowing if a media file is manually attached to a post or page, but it’s not perfect and can miss some).
For example, if you uploaded an image to WordPress and then used the direct link to “embed” it to a post or a page, if you check the list of unattached media files in the Media Library, you’ll see that your manually inserted image is still included in the list. Therefore, deleting all unattached media files is not always the best way.
Unfortunately, there is no “one-click” way to make sure that an image is not attached or linked manually somewhere on your website. However, there certainly is a way, albeit a little bit time consuming and a little bit complex. Follow these steps carefully and you’ll be able to safely remove all “truly” unused media files in WordPress.
Once you finished installing the plugins, we’re going to configure Broken Link Checker first. Go to Settings > Link Checker.
In the General tab, follow the settings shown below. Feel free to use your preferred email address in the Notification e-mail address field.
Then on the “Look For Links in” tab, enable all options in Look for links in and Post statuses except for ACF fields and Custom fields, unless you use either of them.
Next, in the Which Links to Check tab, check everything in the Link types section. Configure Exclusion list if you want. Enable everything in the Protocols & APIs tab too. Leave the Advanced tab untouched.
After configuring the plugin, click the “Save Changes” button located at the bottom and you’re done.
You’ll notice that this plugin will begin crawling your website for broken links as soon as you finished installing it and it might even find a few already, but because we are focusing on removing unused media files, you can ignore them for now.
Make sure that you configured Media Cleaner to make use of the Trash feature before continuing. To enable this feature, refer to the tutorial above.
Now that you’ve configured Broken Link Checker and you’ve enabled the trash feature and properly configured settings in Media Cleaner, it’s time to get started.
The idea here is that we’re going to use Media Cleaner to delete all unattached media files and we’re going to take advantage of its Trash feature. Instead of permanently deleting all unattached media files, we’re going to temporarily move them to the trash instead. After that, we’re going to use Broken Link Checker to find broken media links and compare the media names or links to what we have in the trash can. If we found a broken image or image link that has a match in Media Cleaner’s trash, we can easily restore it back. Isn’t it great?
To start cleaning, go to Media > Cleaner.
Click the Start Scan button, and the plugin will start analyzing your website for unused media files.
It may take some time to finish, especially if you have a larger website. Once it finishes analyzing, it will list all unattached media as “issues”. Select them all and click Delete button.
Once it’s done, refresh the page and you’ll see that they were moved into the Trash.
Now we are going to use the Broken Link Checker plugin to check for broken media/image links. Go to Settings > Link Checker. Then in the Advanced tab, scroll down to the bottom and click the Re-check all pages button to refresh the list of broken links. It might take some time to finish.
Once it’s done, you can now check the list of broken links in Tools > Broken Links.
In our example, we have a broken image called “forest-background.jpeg” and it’s from the site itself. We can infer that this is an image that was deleted by Media Cleaner earlier.
Upon checking the list of items in the Trash in Library > Cleaner, we found the said image. Fortunately, we can restore it back with ease.
Go back to Tools > Broken Links and click recheck on the broken image link.
If the restore is successful, you’ll see that Broken Link Checker will update the status to 200 OK meaning that we successfully restored the image.
That’s how you thoroughly clean unused media files in WordPress if you are using an unconventional way to insert media files from your Media Library to your content.
Like what we said earlier, if you often upload high-resolution images, you are using more disk space than you think. Why is that? We’re going to take a deeper look at how WordPress handles images to know.
Take a look at our sample media library below which only has five images uploaded:
Like we said earlier, for each image you upload, WordPress automatically generates “thumbnail copies” of different dimensions. However, these “thumbnail copies” are not visible in the media library.
To see them, go to your wp-content/uploads folder. Note: the image below was taken from a local web server running XAMPP in Windows 10 Pro.
As you can see, there are multiple duplicate images named according to their dimensions. Some of these might not even be displayed on your live site. However, deleting these duplicate images might cause errors, so refrain from doing that.
By using WordPress’s built-in media manager, if you mark an image for deletion, it will automatically delete its thumbnail copies, so you don’t have (and you must not) to delete them manually to save disk space. Manually deleting a thumbnail copy might visually break something on your site.