How to check which images of the media library are not used

Your media library is full of images, do you need all of them?

If you want to know which ones are needed, do the following:


  • Put this code in your functions.php


if( isset( $_REQUEST['checking_images'] ) && isset( $_SERVER ['HTTP_USER_AGENT'] ) ){
    error_log( 'user_agent: '.$_SERVER ['HTTP_USER_AGENT'] );


  • Put this code in your wp-config.php


if( isset( $_REQUEST['checking_images'] ) ){
    define('WP_DEBUG', true);
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false );
    define('UPLOADS', '/no-folder/uploads');


  • Check your website with an online tool to detect the missing images, e.g. But be careful, you need to add ?checking_images to the URL. E.g.
  • Open the file wp-content/debug.log and check what is written after user_agent:
  • Modify the code in wp-config.php:


if( isset( $_SERVER ['HTTP_USER_AGENT'] )  && 'tool user agent' === $_SERVER ['HTTP_USER_AGENT'] ) ){
    define('UPLOADS', '/no-folder/uploads');


where “tool user agent” is the user agent of the tool you found in debug.log. Replace it with the right user agent.


  • Run again the tool to check the images.


The tool will give you a big list of missed images because in wp-config.php you are replacing the path of the media library folder with a non-existing one. That list is also the list of images you need on your website. All the other ones can be deleted.


Be very careful. This method works if the tool to detect the missed images works right and scans all the pages of your website.

Before using this method, make always a full backup including all the files of the media library.