23

I am trying to clean out my uploads folders as they are now starting to take up too much room on my server.

I have tried to use plugins to achieve this end such as DNUI and Cleanup Images but these plugins are either unworkable or in DNUI's case (which has been updated recently and works) do not achieve the exact results that I am after. This is because my site uses Woocommerce and a theme which both use images within other places rather than just posts or pages such as headers, footers and galleries. It seems as if WC uses it's own built in Lightbox plugin called Pretty Photo which is where the images that are detailed as "unused" by DNUI are called from.

I know that it is a bit of an ask but I am wondering if anybody could point me in the right direction? I am no developer, so probably should not even be posting on here, but I can work things out with time and determination. I have seen scripts on this site that achieve the same results as the DNUI plugin but nothing that takes into account other image uses (if it is even possible?). Any help would be much appreciated.

6
  • 2
    I've wanted to do the same thing, but what worries me are how to define unused. For example, an author may have uploaded images to a draft post, and then created another draft post, pasted the content, and that post is published. This will mean that the images are assigned to the draft post, not the published post. Maybe a script that looks through all posts and checks the image URLs to see which images have been used and not. Hopefully we have a good answer.
    – Christine Cooper
    Jun 10, 2014 at 16:14
  • Unfortunately the more elaborate your definition of "unused" is, the less likely you will find fitting solution for it. How many (in count and size) images are you talking about?
    – Rarst
    Jun 10, 2014 at 17:48
  • Hi Rarst, thanks for answering, the site has been active for around 5 years now and is very image dependant, with quite a few changes of theme and plugins. I believe there are literally 1000s of unused images. And because of the amount of changes the sizes vary tremendously. I have already used the Force Regenerate Thumbnails to try and reduce this number and this has worked to an extent but this plugin often times out due to the amount. It also raises the CPU on my server to an unworkable level if I leave it activated.
    – G-Olly
    Jun 11, 2014 at 8:44
  • I realise the complexity of the problem and so I have been using the media library to try and pinpoint and delete these images. Unfortunately I have found that it also often lists used as unused and vice versa, as such it is a painstakingly slow process but I've got my nose to the grindstone now.
    – G-Olly
    Jun 11, 2014 at 9:06
  • 1
    If I may put my two cents in, for some kinds websites I think the problem can be solved empirically. For these sites webcrawlers are visiting every page on the site. From your server log get the list of urls of all visited pages. Parse these pages using DOMDocument and get all <img> elements and their srcs. The problem with this approach is if Javascript dynamically generates hrefs and srcs.
    – user27457
    Aug 15, 2014 at 12:44

5 Answers 5

7

Deleting images using plugin:

You can use this plugin, it will search your database and look if image is inserted into any post (in content, as featured image, in any custom field, anywhere...) or as background...

If image is not used anywhere it will give you option to delete it. You will get list of all images on your site not used anymore, so you can safely delete them.

https://wordpress.org/plugins/dnui-delete-not-used-image-wordpress/

and this, still work, even it is outdated

https://wordpress.org/plugins/wordpress-uploaded-files-cleaner/


Deleting images manually:

You can also search through your media library for images which are unattached to posts and pages.

Go to media library and click the "unattached" it will display all images that may be displayed on other parts of your site or not used.

2
  • 1
    The problem with images that are not attached to any post is that they may be used in some other place - for example if you have a custom field that use a image uploader based on the native Media uploader of Wordpress, depending on how the theme was coded, the image uploaded there is not attached to any post (on your database). Still a valid idea. Jun 18, 2015 at 13:20
  • I'll second @BrunoMonteiro here. It's entirely possible to have a images that are not attached to a post.
    – BODA82
    Oct 11, 2015 at 1:46
3

Cron/Scheduled tasks and a DB crawl is your friend

It would need to be a plugin, you could assign it a wp_cron interval even, not much unlike the one suggested above but with the addition of checking any 'post_content' like field (you will need to identify these, dependent upon your WP environment) for the image's URI/URL. This has the potential to be extremely time consuming / taxing on the system's processes - realistically this process would only need to run once a day or week - assuming there are no other legal/business reasons to run more frequently.

a master-slave DB configuration of source allows this process to be significantly less of a performance detriment, but that is beyond the scope of this question I believe.

2

I tried the Image Cleanup plugin and it's the best, but still good to make a backup first.

1

You would need to scan your wp-content/uploads folder for uploaded files, and then your database for references to those files, and finally match them together. What can't be matched should be safe to remove.

The database scanning is the tricky part. Most of the file references are stored inside the Media Library which you can access using the standard WordPress functions/API. But what happens to files that are removed from the Media Library but are still referenced by posts? Or files that other plugins link to in different ways?

I did a considerable number of tests and from what I gathered, all file references are either stored in plain-text format (i.e. just a table cell equal to the file path or URL), HTML format (i.e. the content of a post), as serialized data, or finally as JSON objects. You would have to scan the entire database and try to guess what format is used within each cell. Of course, there might be other exotic ways that some plugins are using to keep references to their files, but those can only be handled on a case-by-case basis.

Consequently I wrote a plugin that automatically does all of that, since I needed it for some of our clients' websites, and named it Theia Upload Cleaner. It has worked well for me, but of course you should always make a backup before trying something like this.

2
  • Nice approach... I wonder if you encountered any problem with it with more and more content / plugins / posts added over time...
    – Redoman
    Jun 13, 2018 at 8:24
  • There are issues here and there, yes. For example, for some plugins you need to exclude certain tables that otherwise can slow things down considerably. We do try to automatically exclude by certain keywords, like "log" tables, but it doesn't catch all cases of course.
    – liviucmg
    Jun 13, 2018 at 14:21
0

Media Cleaner WordPress Plugin is pretty good enough - https://wordpress.org/plugins/media-cleaner/

I like it as it has support for other plugins like Advanced Custom Fields (ACF), Divi Builder, Visual Composer and Elementor. (Only in the paid version)

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