I'm looking for ways to clean out the image directories for a site which has become overloaded with unused images and thumbnails. Would it be possible to use wget to only download the images which are referenced by the html pages on the site? I notice that it's possible to browse the download folder and see the files listed, so I presume a straight wget -r is going to download the lot. How would one use wget, but exclude crawling the uploads directory?

1 Answer 1


Your asking for command line solution, I don't have one - but for PHP you can cycle through all posts, build an array of files, compare it to an array of the media library, find unused and delete.

Cycle through Posts

use get_posts() for pages andor posts, and build your foreach loop

Extract Media From Posts

while in the loop find the media items and build a big array of used media urls

function myplugin_find_media($content) {

    $mediaRegex = "/(src|href)=\"(.+?).(jpg|png|pdf|gif)\"/i"; // your support filetypes
    $mediaFind = preg_match_all($mediaRegex, $content, $media);

    if (isset($media[2]) && count($media[2]) > 0)
        return $media[2];

    return false;

Get Library Items

You can get all library items with somthing like

// get the media library for comparison
$library = array();
$args = array(      
    'post_type' => 'attachment',
    //'post_mime_type' => 'image', // if theres only one
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => null,
$attachments = get_posts($args);
foreach ($attachments as $post)
    $library[$post->ID] = wp_get_attachment_url($post->ID); // or add more info about thumbnails, etc

Compare library and Used Media Items

You can then loop through your library and compare it to your extracted used media items.

Note you'll need to factor in the use of thumbnails and resized -###-### images.


With a library media item not being in any posts, and you've got it's post ID as the key in $library, you can use wp_delete_post() to remove it completely.


If this site has a ton of extra thumbnails from different plugins and themes that have created their own sizes, a plugin like this can clean it for you.


Looks like there's a plugin that does this already: https://wordpress.org/plugins/media-cleaner/

  • Yes, we've tried that one, with ... mixed results - it deleted a whole lot of images which were in use. I'm not completely sure that wasn't operator error, but the powers that be have banned it for us.
    – Pauly
    Commented Dec 16, 2017 at 0:13
  • The delete thumbnails plugin works well, but can actually result in more image cruft as after running it you need to regenerate all thumbnails meaning you get every single image size for every image. My thought was that you could pull a copy of the upload directories with only the used images included. I realise that there's a potential problem there in that if an editor creates a post using an old image and uses it in a size that hasn't been kept the image will be missing, but for a lot - say five years - of our old content there's very little chance anybody is going to use the images again.
    – Pauly
    Commented Dec 16, 2017 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.