Delete Unused Images on WordPress – A Complete Guide

WordPress unused images works like a silent drag on your website’s performance. They accumulate over time, bloating your media library and slowing down your site. Manually identifying and removing these digital deadweights is a tedious task that can be prone to errors.
Let’s change that. In this blog, we’ll explore easy strategies to detect and WP remove unused images from your site to give it a much-needed speed boost!
Importance of Removing Unused Images
There are multiple downsides to keeping WordPress unused images in your site’s media library, and while it might not be instantly noticeable, it does eventually slow down the loading speed. Here are some benefits to removing WordPress unused images from your site –
Better Site Management
Regularly removing WordPress unused images helps streamline your website’s content, which makes it easier to manage and maintain only the relevant and necessary files. If you’re looking for improve your site’s performance and organization, you must clean WordPress media library of any photos left unused.
Saving Hosting Costs
WP remove unused images helps you reduce the amount of storage your site requires. This can help save your budget, particularly if you’re using a hosting service that charges based on storage space. Lower storage space equates to faster loading times, meaning better overall user experience.
Easier and Faster Backups
Backing up your site on a daily basis is highly recommended to avoid loss of important data. Websites with fewer unnecessary WordPress unused images will be will be much easier to backup. This will not only save time but also reduce the risk of backup errors or complications in the future.
Quicker Migration
When it’s time to migrate your site to a new host or platform, having fewer images or files makes the process much faster and way less complex. A leaner file structure minimizes the potential for errors during the migration, resulting in a smoother transition with less downtime.
Find and Delete Unused Images in WordPress
You can WordPress delete unused images using a media library plugin or just a custom code. If you’re just starting out with WordPress, using a premium plugin can help you streamline the process. Here’s are a few methods on how to delete unused images in WordPress –
Before starting with method 1, make sure you have both ThumbPress and ThumbPress Pro installed on your website.
Method 1: Using WordPress Image Management Plugin
The easiest method to WordPress delete unused images is by using a dedicated plugin. With ThumbPress, you’ll learn how to find unused images in WordPress, and delete them quickly and effectively.
Step 1: Activate the Plugin
When both plugins are installed, activate them by clicking on the Activate button from your site dashboard.

Step 2: Enable Modules
After that, head to ThumbPress > Modules and enable the Detect Unused Image from the modules page.

Step 3: Detect Unused Images
When the module is enabled, a sub-menu will appear under ThumbPress in the sidebar. From there, go to ThumbPress > Detect Unused Image.

You’ll discover a field from where you can choose how many images you want to detect per request. There are also buttons available to detect images in real-time or in the background. Put the number of images you want to detect at a time and click the Detect Unused Image button.

Step 4: Remove Unused Images from WordPress
Once the unused images have been identified, you’ll get a confirmation message displayed on the right of the Detect Unused Images page. Now click the View Unused Images.

You’ll be redirected to a new page where all the WordPress unused images in your media library will be displayed. Now you can delete them one by one or at once by selecting as bulk.

Method 2: Removing Unused Images Using Code
This method may seem complicated for users unfamiliar with coding, but it’s actually quite simple. There are several ways to WP remove unused images from your WordPress site. For example, by adding PHP code to the functions.php file, you can easily delete images from WordPress media library.
Follow the steps below to learn how to delete multiple images in WordPress media library by adding a PHP code.
Step 1: Delete Unused Image Attachments
When you delete any post from your WordPress site, the associated media are not deleted automatically. All of them remain stored in your server, consuming your server space and slowing down your website speed.
The function below will help you permanently delete those attachments to help WordPress clear media library –
function delete_unattached_attachments() {
// Get all unattached attachments
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'fields' => 'ids',
'post_parent' => 0,
));
// Check if there are any unattached attachments
if (!empty($attachments)) {
foreach ($attachments as $attachment_id) {
// Get the file path of the attachment
$attachment_path = get_attached_file($attachment_id);
// Delete the attachment from the database
wp_delete_attachment($attachment_id, true);
// If the file exists, delete it from the disk
if (file_exists($attachment_path)) {
unlink($attachment_path);
}
}
}
}
Step 2: Delete Custom Post Type Attachments
If you want to remove all images linked to specific Custom Post Type, you need to use the function given below. This function will effectively remove both the attachment from your site, as well as from your web server.
Note: Delete all attachments attached to specified custom posts (card in this case). Don’t forget to replace “card‘ with the name of your custom post type.
function delete_cpt_attachments() {
// Get all attachments
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
));
// Check if there are any attachments
if (!empty($attachments)) {
foreach ($attachments as $attachment) {
$parent_id = $attachment->post_parent;
// Check if the parent post is of the 'card' custom post type
if (get_post_type($parent_id) === 'card') {
$attachment_id = $attachment->ID;
$attachment_path = get_attached_file($attachment_id);
// Delete the attachment from the database
wp_delete_attachment($attachment_id, true);
// If the file exists, delete it from the disk
if (file_exists($attachment_path)) {
unlink($attachment_path);
}
}
}
}
}
Step 3: Delete Blank Thumbnails from the Media Library
If you’re encountering issues with images not displaying in WordPress or receiving a 404 error page, the following step might resolve the problem. This type of issue occurs when the attachments no longer exists on your web server.
Here is the function to fix that –
function delete_404_attachments() {
// Get all attachment IDs
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'fields' => 'ids'
));
// Check if there are any attachments
if (!empty($attachments)) {
foreach ($attachments as $attachment_id) {
// Get the attachment URL
$file_url = wp_get_attachment_url($attachment_id);
// Get the HTTP headers for the file
$file_headers = @get_headers($file_url);
// Check if the file returns a 404 Not Found status
if ($file_headers && strpos($file_headers[0], '404 Not Found') !== false) {
// Delete the attachment from the database
wp_delete_attachment($attachment_id, true);
}
}
}
}
Recommended Tips to Clean Out Your WordPress Media Library
Outside of the two methods mentioned in this blog, you can also follow some general tips to remove unused images WordPress effectively. Below are some pro tips to help you clean up WordPress media library and keep your data storage in working order –
- Before starting with any of the process, make sure you have a backup of your WordPress site.
- When you upload any images you don’t want, remove them immediately after posting.
- Convert your Images before uploading them to your website.
- Before adding the code in functions.php use a child theme or a code snippet plugin.
- Regularly monitor your site’s posts and media library. Remove if there are any unused attachments.
Wrapping Up
Keeping your WordPress media library organized is essential to keep your site running at its highest potential. Regularly identifying and removing unused images can dramatically improve load times and free up valuable storage space. With ThumbPress, you can use WordPress find unused images, and clean up your media library with just a few clicks.
So, learn how to find unused images in WordPress, select your preferred method, and clean up your site in minutes!
Frequently Asked Questions (FAQs)
Q. How do I delete all unused images in WordPress?
Follow the steps below to delete all unused images WordPress –
- Install and activate ThumbPress and ThumbPress Pro on your website
- Enable the Detect Unused Image module from the site dashboard
- Go to detect unused tab and provide the number to detect unused image
- Click on Detect Now button
- All the unused images will be displayed on your site.
- After that, remove all the unused images.
Q. How do I find Unused images in WordPress?
Go to your site media library and you can see all the uploaded images. From there click on the dropdown menu and select Unattached.
Q. How do I delete duplicate Images in WordPress?
There are no instant solutions for this. You have to use the WordPress plugin Media Deduper to remove duplicate images from your website. This plugin will allow you to safely delete duplicate images from your media library.
Subscribe to Our Newsletter
Get the latest WordPress tutorials, trends, and resources right in your inbox. No Spamming, Unsubscribe Anytime.

Thank you for subscribing to our newsletter!
Table of Content
- Importance of Removing Unused Images
- Better Site Management
- Saving Hosting Costs
- Easier and Faster Backups
- Quicker Migration
- Find and Delete Unused Images in WordPress
- Method 1: Using WordPress Image Management Plugin
- Method 2: Removing Unused Images Using Code
- Recommended Tips to Clean Out Your WordPress Media Library
- Wrapping Up
- Frequently Asked Questions (FAQs)
- Q. How do I delete all unused images in WordPress?
- Q. How do I find Unused images in WordPress?
- Q. How do I delete duplicate Images in WordPress?