0

To clean up my media library, I deleted some unused pictures directly out of my uploads folder. Now, whenever I go to the media library the file names are still there but the thumbnails have been deleted: https://snag.gy/lzCLiF.jpg

Where is the image metadata stored on my server so that I can delete it there.

1

The image url is store in wp_posts as an attachment post_type with their corresponding posts(s) as parents. From Codex, see Template Tags Show All Attachments

I believe something like this would retrieve all attachments in DB outside of a loop:

   global $post;
   $args = array( 
         'post_type' => 'attachment', 
         'posts_per_page' => -1, 
         'post_status' => 'any', 
         'post_parent' => null 
          ); 
   $attachments = get_posts( $args );
  if ( $attachments ) {
    foreach ( $attachments as $post ) {
        setup_postdata( $post );
        the_title();
        the_attachment_link( $post->ID, false );
        the_excerpt();
    }
    wp_reset_postdata();

Perhaps something like this could find and clear those no longer available in /uploads/ directory:

$args = array(
      'post_type' => 'attachment',
      'numberposts' => -1
      );
$attachments = get_posts( $args );
foreach( $attachments as $attch ){
    $file = get_attached_file( $attch->ID );
    if( !file_exists( $file ) ){
        wp_delete_post( $attch->ID, false );
    }
}

Reference for get_attached_file()

Reference for wp_delete_post()

Look into those those and it should be rather straightforward how to achieve the cleanup you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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