Skip to content

Storage: Remove un-attached images #346

@luukee

Description

@luukee

Issue: "On our WordPress site we need to identify unused images and delete them because our hosting storage plan is at its max."

Chat GPT response:


Step 1: Backup Your Site

Before deleting anything, create a full backup of your site, including the database and wp-content/uploads folder, in case something goes wrong.


Step 2: Identify Unused Images

Option 1: Use a Plugin

  1. Install a Plugin
    Use a plugin like [Media Cleaner](https://wordpress.org/plugins/media-cleaner/) or [Advanced Database Cleaner](https://wordpress.org/plugins/advanced-database-cleaner/).

    • Media Cleaner scans your media library and identifies unused images.
    • It cross-references images with posts, pages, and custom post types to detect files that are not in use.
  2. Run a Scan

    • Go to the plugin settings.
    • Perform a scan to identify unused media files.
  3. Review the List

    • Carefully review the list of unused images before deleting them.

Option 2: Use WP-CLI (Advanced)

If you are comfortable using WP-CLI, you can list unattached media files:

wp media list --status=unattached

This will show images that are not attached to any post or page.

Option 3: Manual Review (Time-Consuming)

  • Go to Media > Library in the WordPress admin.
  • Switch to the "List" view and sort by the Uploaded To column.
  • Look for files with "Not attached" and manually verify if they're still in use.

Step 3: Delete Unused Images

Using Plugins:

  • After identifying unused files, plugins like Media Cleaner allow you to delete them in bulk.

Using File Manager or FTP:

  1. Access the Server
    Use cPanel's File Manager or an FTP client like FileZilla.
  2. Navigate to wp-content/uploads/
    Review old image folders for unused or orphaned files.
  3. Delete with Caution
    Cross-check files before deleting to ensure they’re not hardcoded in themes or custom post types.

Step 4: Optimize Remaining Images

  1. Use a plugin like [Smush](https://wordpress.org/plugins/wp-smushit/) or [ShortPixel](https://wordpress.org/plugins/shortpixel-image-optimiser/) to compress images.
  2. Remove unnecessary image sizes generated by WordPress using the remove_image_size function in your theme's functions.php.

Step 5: Prevent Future Clutter

  1. Figure out what image sizes are not used in our theme
    First we need to find what image sizes are used for each page in our theme code. Image sizes in WordPress

  2. Disable Unnecessary Image Sizes:
    Add this to your functions.php file to prevent WordPress from generating certain image sizes:

    add_filter('intermediate_image_sizes_advanced', function($sizes) {
        unset($sizes['medium']);
        unset($sizes['large']);
        unset($sizes['thumbnail']);
        return $sizes;
    });
  3. Use a Media Library Organization Plugin:
    Plugins like [Media Library Organizer](https://wordpress.org/plugins/media-library-organizer/) can help keep your media library clean going forward.


Final Recommendations:

  • Start with plugins to avoid manual effort.
  • Review the list of unused images carefully to avoid accidentally deleting important files.
  • Once unused images are deleted, monitor your storage regularly to prevent future issues.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions