Delete Unused Images on WordPress – A Complete Guide

Aug 13, 2024

Share this content

Copy Link
Share

Unused images are 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 and error-prone task.

Let’s change that. In this blog, we’ll explore easy strategies to detect and remove unused images from your WordPress site to give it a much-needed speed boost!

Importance of Removing Unused Images

There are multiple downsides to keeping unused images in your site’s media library, and while it might not be instantly noticeable, it does take a significant toll on your overall website speed. Here are some benefits to removing unused images from your WordPress site –

Better Site Management

Regularly removing unused images helps streamline your website’s content. This makes it easier to manage and maintain only the relevant and necessary files. If you’re looking for better site performance and organization, removing unused images is the way to go.

Saving Hosting Costs

Deleting unused images helps you reduce the amount of storage your site requires. This can significantly save on costs, 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

Websites with fewer unnecessary files, such as unused images, will be quicker and easier to back up. This can save time and 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 faster and 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 easily delete unused images from your website using a media library plugin or custom code. If you’re just starting out with WordPress, using a premium plugin can help you streamline the process. Let’s start with the WordPress image management plugin.

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

Step 1: Activate the Plugin

When both plugins are installed, activate them by clicking on the Activate button from your site dashboard.

install-plugins

Step 2: Enable Modules

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

enable-detect-unused-images

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.

Detect-Unused-image-menu

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.

delete-unused-images

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.

View-Unused-Images

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

delete-unused-image

Method 2: Removing Unused Images Using Code

This method might seem complicated for users unfamiliar with coding, but it’s actually quite simple. There are several ways to remove unused images from your WordPress site. For example, by adding PHP code to the functions.php file, you can easily delete unused images from your media library. So don’t hesitate—get started now!

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 –

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.

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 below 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

Here are some recommended steps for you to keep your WordPress Media Library clean and organized.

Follow these tips to reduce load times 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 and efficient is crucial for optimal website performance. Regularly identifying and removing unused images can dramatically improve load times and free up valuable storage space.

With the right tools and strategies, this task becomes manageable and even enjoyable, ensuring a clean media library and a faster website.

Frequently Asked Questions (FAQs)

Q. How do I delete all unused images in WordPress?

Follow the steps below to delete all unused images from 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.

Please fill out the empty field.

preloader

Thank you for subscribing to our newsletter!

Mustakim Ahmed

A seasoned Business Developer & Support Engineer who is deeply passionate about resolving WordPress-related issues and contributing to WordPress. His expertise lies in enhancing user experiences and driving success within the WordPress ecosystem.

Leave a Comment

Your email address will not be published. Required fields are marked *

Get ThumbPress

Boost site speed, save server space, and increase sales by smartly managing WordPress images.

Get Now
Scroll to Top