Updates: 1. Code updated to detect Background Images 2. Code updated to detect Site Icon 3. Code updated to detect Favicon 4. Code doesn't append delete_delete_etc each time you run/search for delete :) 5. Button added so you can remove the prefix delete_ as you may not need them now :) 6. It now covers Product Categories too If you can make this code even better - send it to me - so we can update the current code. This is the start - but some of you will spot gaps so get cracking with your coding. And remember - if you don't like it - don't use it. Stay safe.
Nice work but, highlight a lot of images which are in fact used ! for exemple images used by Slider ( slider revolution) are not identified as used image. So it take pretty much the same time to check all highlighted images before deleting any. At least it is so for me, by chance I did it only on staging to try and have been back to backup after deleting pretty much all my images ;) I mean, the unused concept is not good enough to be really useful in my case.
@@fredvictor6890 It's useful for quite a few but I accept that it's not great for every scenario. The code is there to be built upon and I look forward to seeing what others do with it :)
Another Code Snippet Magic... If I count how many times you have saved me with your code snippets Codes!!!!! You are a Gem in this Industry... Thank You millions
This is great! One step further, there's a bulk download plugin that would allow you to download all the "delete_" ones, and you can offline archive all the images prior to actually removing them from the site (especially if it's a site that you inherited and don't have 100% familiarity with). Cheers man!
Absolutely brilliant Imran. Been looking for something like this for ages. A town council arts website I host has over 2000 images now down to less than 400 after checking that none are recent and not being used on a test site. Now off to do the real site. Thanks for your extraordinary work you do for no gain.
Imran, Are you able to check that images used in JetEngine Post Types Gallery are not affected as think it may have deleted some as just getting a place holder now. Did not notice this before as only a few posts exist using that method and forgot to check. Not a problem as still on test site. Thanks.
Thanks, man! I was to sort this out for a while now, but with so many stuff to do prior to it, just hadn't gotten to it yet; now you just delivered it...
Might give false positives if images are added via other plugins or stuff but you’re in control over what you remove and it now comes with an extra button to remove the prefix too
@@websquadron Yes, I'm just experimenting. Looks like it is not seeing my images on JetEngine galleries which are displayed via a template on custom posts. So definitely needs to be done manually. The other thing it is possibly not catching is images loaded in things like popups. And maybe, I'm not sure yet, where I've got different images loaded for mobile or tablet view. But still very useful. And easier than guessing.
What a legend, thanks so much for this!! I just took on a simple WP website that is 1GB in a size. You can guess where the space is taken up...yup..old images and loads of them. Time for a staging site 😊😊 Thanks again!
Thanks Imran. This is really good. I need to create a staging site to test this. However, with the default settings, when you upload an image, Wordpress creates 3 variations (Large, Medium and Thumbnail). I have an old site built with Enfold which actually creates 14 variations (and you have no option to change this). A shortpixel optimised image upload (67KB) takes up 3.887 MB with all the variations. Two questions: 1. When deleting an image, does the snippet also delete the variations? 2. If you ARE using an image, could the snippet delete the variations not in use? As always, thanks for everything you do.
Variations may still exist but you should add this snippet to just keep the original and the thumbnail: ua-cam.com/video/ceQv9FgIZmQ/v-deo.htmlfeature=shared
@@websquadron and therein lies the problem. Even when you disable these settings, Enfold still creates additional versions. There is no way to see these other than viewing them directly on the server. So how do you remove old unused images and their unwanted variations? Or unwanted variations of images that are in use? Perhaps that's a 2025 challenge for you :)
@@LucyVozenilek Not a challenge for me :) Too many variables to handle and too many alternate tools. I always use manual processes to handle my webp images, and ensure I have this snippet activated to stop duplicates from the get-go. For legacy sites you have to manually delete from your WP Files and Databases. Never fun :(
Hi, I came upon the code to remove unused java script: //This has been syncronised 123// 2function wp_remove_scripts() { 3// check if user is admina 4 if (current_user_can( 'update_core' )) { 5 return; 6 } 7 else { 8 // Check for the page you want to target 9 if ( is_page( 'homepage' ) ) { ..... After reading through I noticed that it only removes indicated java script on the 'homepage' To remove from entire site can the individual pages be listed separated by commas and quotes is there is another way to do it? Thanks.
That's the beauty of the code :) You can see what's not been picked up so you don't delete something accidentally. BUT - i just updated to make sure it identifies the Site-Icon and Favicon - note that it keeps the version/cropped version used :)
Tested on a staging site and worked great except for a container background slideshow. Did not recognize images as being used. This is something I have been looking for quite some time.
Great! Can we have the same solution for plugins? In some of my big websites I've used a lot of plugins that I'm not sure if I've used them anywhere in my website and now I'm afraid to deactivate it
i tried running this on our site Imran mate, and an error occurred. i think it is probably because we have 32000 images. is it possible to get it to run in smaller batches you think, so it doesnt crash the server?
@@websquadron superstar imran - i will keep an eye out, or reply to this comment when u get it done so i can test it. good night for the leeds fans last night
@@websquadron We have 7 images generated. We need them all. We're a photography website and promote photography tours, workshops, sell products, have ads and backgrounds etc. We have galleries that expand on click to full screen size 2560x in a number of places too. I generate the proper image size for the place it's used for performance reasons. I have no issue generating the images. Im more curious about ways to find unused , through custom code, widgets, pretty much everywhere. I've looked into this many times and it's a complex issue (for us anyway). This functionality is on the HappyFiles Roadmap, but not out yet.
That's a pity :( I haven't got the resources to check against all of them - but you have the code to build upon :) Some plugins modify the filenames when they are used hence it can become very subjective to identify them.
this is faboulous, thank you. But I just tried it on my staging site and noticed that it's also marking product-category pictures which are in use for deletion. Can you confirm or is it just a bug on my site? thank you.
I've updated it :) 1. Code updated to detect Background Images 2. Code updated to detect Site Icon 3. Code updated to detect Favicon 4. Code doesn't append delete_delete_etc each time you run/search for delete :) 5. Button added so you can remove the prefix delete_ as you may not need them now :) 6. It now covers Product Categories too
@@websquadron you're so smart, thank you. That works. For some reason it does not detect pictures in my blog being in use and marks them for deletion. It's not a big deal, as I can check that manually. It certainly is exactly what i was looking for, because i have so many pictures blowing up my website. Again thank you so much for this great snippet ;-)
Tried this - first thing the code snippet did was to pre-fix Delete for my homepage banner so failed at the first hurdle. The image was in an elementor slideshow.
The code is the start :) One day it will be perfected by someone - but at least you know not to remove it. Plus the code gives an extra button to remove the Delete_ from the title for the false positives. Thanks for giving it a go.
I must be missing something - doesn't work for me. I click the "mark unused images" button, see a screen blip, but nothing is marked delete. image analysis complete. Used Images: 1723. Unused Images: 48. Using Elementor Pro
This didn't work for me. Deleted half of my used images - Elementor content, Elementor sliders, Gutenberg content - everywhere. Glad I followed your advice and made a staging first ...
Unfortunately it doesen't work for me. Images were taken from the widgets gallery and image carousel was not recognized and therefore deleted. Good thing it was a test site.
Hi brother, I tried to test the snippet on a staging website. But it is picking the images that are being used in the IMAGE widget of elementor. Not sure why it happened.
@@websquadron The issue is that the code is selecting the images used on the website and appending the word "Delete" to their names. Specifically, I have an image widget on the blog page that is populated with dynamic data. However, the code snippet is also picking the images displayed in that blog page image widget.
Great idea, but unfortunately, it doesn’t work very well on my site. I’m using a third-party plugin with numerous option images, and this code highlighted many of them, adding ‘delete’ to the image file titles. So do check it thoroughly before bulk deleting the highlight images! I hope I can program so that get the code to better suit my site. 😴 Anyway, thank you for the video. It seems the only option for me might be to manually delete the unused images one by one. 😭
Iman bro I wish you had a visual designer on your team so I could watch your vids all the way thru (sorry, you rock, but you cut so many corners on rushed showcase tutorials w no regard for proper visual best visual presentation) again I had to click away half way thru. Just being honest brah! 4:19
@@websquadron Yes. You find the image in Elementor, edit container, style, background. I have probably 8 sections on the homepage all with their own background image. All the images were highlighted as not used and subsequently, I manually deleted them by searching for all 'delete', selecting them, and then permanently delete. No harm done, just restored my backup.
Unfortunately, this does not work on my site (test site). I have created some CPTs with ACF. All my CPTs are “active and public”. I have checked it with: ” /** * Plugin Name: Post Types Overview Dashboard Widget * Description: Adds a dashboard widget displaying all registered post types, their public status, and post counts. * Version: 1.0 * Author: Your Name */ // Hook into the dashboard setup action add_action('wp_dashboard_setup', 'add_post_types_overview_dashboard_widget'); function add_post_types_overview_dashboard_widget() { wp_add_dashboard_widget( 'post_types_overview_widget', // Widget slug. 'Post Types Overview', // Title. 'render_post_types_overview_widget' // Display callback. ); } function render_post_types_overview_widget() { // Retrieve all post types as objects for easy access $post_types = get_post_types(array(), 'objects'); echo ''; echo 'Post TypePublicPublished Count'; echo ''; foreach ( $post_types as $pt ) { // Count published posts for this post type $count = wp_count_posts($pt->name); $published_count = isset($count->publish) ? $count->publish : 0; echo ''; echo '' . esc_html($pt->labels->name) . ' (' . esc_html($pt->name) . ')'; echo '' . ($pt->public ? 'Yes' : 'No') . ''; echo '' . intval($published_count) . ''; echo ''; } echo ''; echo ''; } ” This creates a table with “all active Post Types” on the dashboard. Do you know why some of the images are labeled “delete” even though they are “Uploaded to”?
Updates:
1. Code updated to detect Background Images
2. Code updated to detect Site Icon
3. Code updated to detect Favicon
4. Code doesn't append delete_delete_etc each time you run/search for delete :)
5. Button added so you can remove the prefix delete_ as you may not need them now :)
6. It now covers Product Categories too
If you can make this code even better - send it to me - so we can update the current code. This is the start - but some of you will spot gaps so get cracking with your coding. And remember - if you don't like it - don't use it. Stay safe.
by the way, im getting "Error 400: redirect_uri_mismatch" when trying to login using my gmail account to get the code
@@Digital-view24 www.codesnippets.cloud/snippet/WebSquadron/delete-unused-images
Works for me. Did you click or copy the link
Nice work but, highlight a lot of images which are in fact used ! for exemple images used by Slider ( slider revolution) are not identified as used image. So it take pretty much the same time to check all highlighted images before deleting any. At least it is so for me, by chance I did it only on staging to try and have been back to backup after deleting pretty much all my images ;) I mean, the unused concept is not good enough to be really useful in my case.
@@fredvictor6890 It's useful for quite a few but I accept that it's not great for every scenario. The code is there to be built upon and I look forward to seeing what others do with it :)
@@websquadron fair enough ! :)
Another Code Snippet Magic... If I count how many times you have saved me with your code snippets Codes!!!!! You are a Gem in this Industry... Thank You millions
tested on staging site first, then on live website, works perfectly and deleted 600 unused images
Great thanks Imran
Great 👍
Perfect timing for this code snippet.
Your info has always come at the right time.
This is great! One step further, there's a bulk download plugin that would allow you to download all the "delete_" ones, and you can offline archive all the images prior to actually removing them from the site (especially if it's a site that you inherited and don't have 100% familiarity with). Cheers man!
Hum that's a good idea. ...i will use our friend chat to see the code for that
Yes - a bulk download of the marked images as an archive before hitting the 'delete' would be awesome. Removes the stress of making a mistake.
Are you saying there is such a plugin, or would it be nice if there was one? If there is one, what is it called? I do not see something obvious?
@ Media Library File Download
By Calculabs
Interested in this as well. Is there a plugin?
I'm just here grinning.... I love when you get excited about a solution!
Hats off, Imran. That's an amazing utility!
This is genius! One less plugin to have to use. Well done Imran!
Worked so smoothly, thanks Imran. from 1000+ photos down to 300. happy days.
Oh wow!!!!
Absolutely brilliant Imran. Been looking for something like this for ages. A town council arts website I host has over 2000 images now down to less than 400 after checking that none are recent and not being used on a test site. Now off to do the real site. Thanks for your extraordinary work you do for no gain.
Imran, Are you able to check that images used in JetEngine Post Types Gallery are not affected as think it may have deleted some as just getting a place holder now. Did not notice this before as only a few posts exist using that method and forgot to check. Not a problem as still on test site. Thanks.
Worked perfectly! Cleaned up a few hundred unused images. Thank you!
Excellent!
Thanks, man!
I was to sort this out for a while now, but with so many stuff to do prior to it, just hadn't gotten to it yet; now you just delivered it...
Imran, you are incredible. Thank you for such a great video.
Dziękujemy.
Thanks :)
Oh wow! Thanks Imran, this is amazing. Off to spin up a staging now and see what I can do for my image-heavy sites. Awesome.
Might give false positives if images are added via other plugins or stuff but you’re in control over what you remove and it now comes with an extra button to remove the prefix too
@@websquadron Yes, I'm just experimenting. Looks like it is not seeing my images on JetEngine galleries which are displayed via a template on custom posts. So definitely needs to be done manually. The other thing it is possibly not catching is images loaded in things like popups. And maybe, I'm not sure yet, where I've got different images loaded for mobile or tablet view. But still very useful. And easier than guessing.
I’ve been praying for something like this. Thank you!
This is genius Imran!!!!!
Great solution Imran! Thank you for your amazing work!
What a legend, thanks so much for this!! I just took on a simple WP website that is 1GB in a size. You can guess where the space is taken up...yup..old images and loads of them.
Time for a staging site 😊😊
Thanks again!
Hope it works for you.
Note that duplicate images in your wordpress files may still remain.
Exactly what I needed. Thanks, Imran.
Oh my god, literally, I was looking for this.
Great job, thank you!!
Just awesome Man ❤
You're a life savior 🙌🏻
Really good work!
merry christmas!!!
kai
Thx Imran! big up
Imran, shear magic - thank you
Cool ! Thanks
good one!
Thank you so much!
This is so genius!👏
Test on a staging site.
very interesting. I will give this a try on a staging site.
Thanks Imran. This is really good. I need to create a staging site to test this. However, with the default settings, when you upload an image, Wordpress creates 3 variations (Large, Medium and Thumbnail). I have an old site built with Enfold which actually creates 14 variations (and you have no option to change this). A shortpixel optimised image upload (67KB) takes up 3.887 MB with all the variations. Two questions: 1. When deleting an image, does the snippet also delete the variations? 2. If you ARE using an image, could the snippet delete the variations not in use?
As always, thanks for everything you do.
Variations may still exist but you should add this snippet to just keep the original and the thumbnail: ua-cam.com/video/ceQv9FgIZmQ/v-deo.htmlfeature=shared
@@websquadron and therein lies the problem. Even when you disable these settings, Enfold still creates additional versions. There is no way to see these other than viewing them directly on the server. So how do you remove old unused images and their unwanted variations? Or unwanted variations of images that are in use? Perhaps that's a 2025 challenge for you :)
@@LucyVozenilek Not a challenge for me :) Too many variables to handle and too many alternate tools. I always use manual processes to handle my webp images, and ensure I have this snippet activated to stop duplicates from the get-go. For legacy sites you have to manually delete from your WP Files and Databases. Never fun :(
Brilliant !!!
Truly time savior. Can this work for pdf files as well?
Just for images for now
Hi, I came upon the code to remove unused java script:
//This has been syncronised 123//
2function wp_remove_scripts() {
3// check if user is admina
4 if (current_user_can( 'update_core' )) {
5 return;
6 }
7 else {
8 // Check for the page you want to target
9 if ( is_page( 'homepage' ) ) {
.....
After reading through I noticed that it only removes indicated java script on the 'homepage'
To remove from entire site can the individual pages be listed separated by commas and quotes is there is another way to do it?
Thanks.
I've used the snippet, works all good except site logo and favicon.
That's the beauty of the code :) You can see what's not been picked up so you don't delete something accidentally. BUT - i just updated to make sure it identifies the Site-Icon and Favicon - note that it keeps the version/cropped version used :)
@@websquadron That's great work, appreciate the attention to details. Thank you!
Hamburger and Close SVGs used as dynamic tags in Off Canvas are prefixed "Delete."
Any false positives can be ignored, but you also have the button to remove the prefix after you've dealt with the real Deletes.
Brilliant! I love it!!!
It may not work for some as sites can be very different so test on a staging site.
@@websquadron of course!
Tested on a staging site and worked great except for a container background slideshow. Did not recognize images as being used. This is something I have been looking for quite some time.
Hopefully someone can advance the code. At least you know they are there so don’t delete them.
Excellent. Media libraries get huge before ya know it. Scary to use some media cleaners. Gonna use Imram snippet solution
Use on a staging site to test
Great! Can we have the same solution for plugins? In some of my big websites I've used a lot of plugins that I'm not sure if I've used them anywhere in my website and now I'm afraid to deactivate it
That could take some work to get right.
i tried running this on our site Imran mate, and an error occurred. i think it is probably because we have 32000 images. is it possible to get it to run in smaller batches you think, so it doesnt crash the server?
I was working on a version that runs in batches, but will revisit
@@websquadron superstar imran - i will keep an eye out, or reply to this comment when u get it done so i can test it. good night for the leeds fans last night
Is this deleting all sizes generated or just the main image?
Just the main. You need to have another snippet that ensures only main and 150x150 are created.
@@websquadron We have 7 images generated. We need them all. We're a photography website and promote photography tours, workshops, sell products, have ads and backgrounds etc.
We have galleries that expand on click to full screen size 2560x in a number of places too.
I generate the proper image size for the place it's used for performance reasons. I have no issue generating the images.
Im more curious about ways to find unused , through custom code, widgets, pretty much everywhere. I've looked into this many times and it's a complex issue (for us anyway). This functionality is on the HappyFiles Roadmap, but not out yet.
Bro this is fantastic Idea 😍
but I think it doesnt work properly with Imagify plugin and image optimizers
That's a pity :( I haven't got the resources to check against all of them - but you have the code to build upon :) Some plugins modify the filenames when they are used hence it can become very subjective to identify them.
this is faboulous, thank you. But I just tried it on my staging site and noticed that it's also marking product-category pictures which are in use for deletion. Can you confirm or is it just a bug on my site? thank you.
Let me check that part. Hopefully you'll spot those and not delete them.
I've updated it :)
1. Code updated to detect Background Images
2. Code updated to detect Site Icon
3. Code updated to detect Favicon
4. Code doesn't append delete_delete_etc each time you run/search for delete :)
5. Button added so you can remove the prefix delete_ as you may not need them now :)
6. It now covers Product Categories too
@@websquadron you're so smart, thank you. That works. For some reason it does not detect pictures in my blog being in use and marks them for deletion. It's not a big deal, as I can check that manually. It certainly is exactly what i was looking for, because i have so many pictures blowing up my website. Again thank you so much for this great snippet ;-)
Tried this - first thing the code snippet did was to pre-fix Delete for my homepage banner so failed at the first hurdle. The image was in an elementor slideshow.
The code is the start :)
One day it will be perfected by someone - but at least you know not to remove it. Plus the code gives an extra button to remove the Delete_ from the title for the false positives. Thanks for giving it a go.
@@websquadron always like to experiment.. it's definitely a good starting point...
This is amazing bro, thanks a lot!!!
Do you have to register on the cloud link to get the code snippet?
Yes as that's where I store the codes.
Also Images used as back ground?
Code updated to make sure it catches the Bakground Image too :)
I must be missing something - doesn't work for me. I click the "mark unused images" button, see a screen blip, but nothing is marked delete. image analysis complete. Used Images: 1723. Unused Images: 48. Using Elementor Pro
Which Page Builder or plugins are you using for images?
@@websquadron Elementor Pro - 3.25.4
@@websquadron Elementor Pro
This didn't work for me. Deleted half of my used images - Elementor content, Elementor sliders, Gutenberg content - everywhere. Glad I followed your advice and made a staging first ...
Unfortunately it doesen't work for me. Images were taken from the widgets gallery and image carousel was not recognized and therefore deleted. Good thing it was a test site.
Always take care before deleting.
Hi brother,
I tried to test the snippet on a staging website. But it is picking the images that are being used in the IMAGE widget of elementor. Not sure why it happened.
Do you mean it's not picking them up as it does for me?
I assume he means images used in the Widgets or Image Module of Elementor. I will also try and update but our woocommerce site has 10000+ images.
@@niralsura Test on a staging site.
@@websquadron The issue is that the code is selecting the images used on the website and appending the word "Delete" to their names. Specifically, I have an image widget on the blog page that is populated with dynamic data. However, the code snippet is also picking the images displayed in that blog page image widget.
@@niralsura That's right.
Great idea, but unfortunately, it doesn’t work very well on my site. I’m using a third-party plugin with numerous option images, and this code highlighted many of them, adding ‘delete’ to the image file titles. So do check it thoroughly before bulk deleting the highlight images! I hope I can program so that get the code to better suit my site. 😴
Anyway, thank you for the video. It seems the only option for me might be to manually delete the unused images one by one. 😭
That's a pain. Which plugin are you using as it may be labelling them differently after it goes through that plugin.
@@websquadron Extra Product Options
@@DoubleLiu-w1r And do you know if that adds additional prefixes/suffixes to image titles?
@@websquadron it adds like this (many delete in the front of the image title): Delete_Delete_Delete_Delete_AK_Head_LS5
@@DoubleLiu-w1r Yes because you're running it multiple times.
Doesn't work if you have a lots of images. Gets console time-out error. Good maybe for short sites.
How many images did you have as I tested on another dummy site that had 632 images.
Iman bro I wish you had a visual designer on your team so I could watch your vids all the way thru (sorry, you rock, but you cut so many corners on rushed showcase tutorials w no regard for proper visual best visual presentation) again I had to click away half way thru. Just being honest brah! 4:19
Really? What was wrong here? Would you rather I was slower or took my time more?
PS - which bit did I cut corners here?
Naa, deleted all my background images. And that was after the updates.
Interesting. Were the images in containers background. It won’t delete unless you manually delete them.
@@websquadron Yes. You find the image in Elementor, edit container, style, background. I have probably 8 sections on the homepage all with their own background image. All the images were highlighted as not used and subsequently, I manually deleted them by searching for all 'delete', selecting them, and then permanently delete. No harm done, just restored my backup.
@@websquadron I replied 2 days ago and I now see the reply went missing? Yes, the image is in the edit container, background, image.
@@Tom59Kriek mine don’t get highlighted but even it does you’re in control whether to delete or not
Unfortunately, this does not work on my site (test site). I have created some CPTs with ACF.
All my CPTs are “active and public”. I have checked it with:
”
/**
* Plugin Name: Post Types Overview Dashboard Widget
* Description: Adds a dashboard widget displaying all registered post types, their public status, and post counts.
* Version: 1.0
* Author: Your Name
*/
// Hook into the dashboard setup action
add_action('wp_dashboard_setup', 'add_post_types_overview_dashboard_widget');
function add_post_types_overview_dashboard_widget() {
wp_add_dashboard_widget(
'post_types_overview_widget', // Widget slug.
'Post Types Overview', // Title.
'render_post_types_overview_widget' // Display callback.
);
}
function render_post_types_overview_widget() {
// Retrieve all post types as objects for easy access
$post_types = get_post_types(array(), 'objects');
echo '';
echo 'Post TypePublicPublished Count';
echo '';
foreach ( $post_types as $pt ) {
// Count published posts for this post type
$count = wp_count_posts($pt->name);
$published_count = isset($count->publish) ? $count->publish : 0;
echo '';
echo '' . esc_html($pt->labels->name) . ' (' . esc_html($pt->name) . ')';
echo '' . ($pt->public ? 'Yes' : 'No') . '';
echo '' . intval($published_count) . '';
echo '';
}
echo '';
echo '';
}
”
This creates a table with “all active Post Types” on the dashboard.
Do you know why some of the images are labeled “delete” even though they are “Uploaded to”?
Variability kicks in with the different plugins.
Nice work