I'd like to run an SQL query to find all featured images from posts and delete the file itself and any references to it.

Can anyone tell me how to accomplish this? Many thanks!

  • Anyone have an update to this? I get the same error.
    – Jae
    Mar 16, 2012 at 20:20
  • @Jae Check my latest edit. I put it in plugin form. Aug 4, 2012 at 15:20

2 Answers 2


A MYSQL Query cannot delete the files themselves. You will have to use wp_delete_attachment(). The following is a proof-of-concept for you to alter how you please. The featured image is stored in postmeta as _thumbnail_id. wp_delete_attachment() does the rest for you.

Plugin Name: Delete All Featured Images
Description: Delete all featured images by visiting /?delete-featured-images=1
Version: 0.1
Author: Brian Fegter
Author URI: http://coderrr.com
License: GPL3v2

# USAGE: visit http://yourdomain.com/?delete-featured-images=1

add_action('init', 'foo_bar_delete_featured', 0);
function foo_bar_delete_featured(){

    # Check for logged in state

    # Check for admin role

    # Check for query string
    if(isset($_GET['delete-featured-images']) && $_GET['delete-featured-images'] == 1){
        global $wpdb;

        # Run a DQL to get all featured image rows
        $attachments = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id'");

        foreach($attachments as $attachment){

            # Run a DML to remove this featured image row
            $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = '$attachment->meta_id' LIMIT 1");

            # Delete attachment DB rows and files
            wp_delete_attachment($attachment->meta_value, true);

            # Print to screen
            show_message('Attachment #$attachment->meta_value deleted.');
  • Thanks for this. I placed it in a PHP file called "remove-featured.php", put that in my root and ran it, but I get the following error: Fatal error: Call to a member function get_results() on a non-object in C:\wamp\www\clients\rusty-rambles\remove-featured.php on line 3 Screenshot: cl.ly/EhqW
    – Alec Rust
    Mar 3, 2012 at 18:15
  • @alecrust you cannot run it outside wordpress, put it in functions.php and make sure you remove it after first usage.
    – Amit Kosti
    Aug 4, 2012 at 14:23
  • @alecrust Check out the latest edit. I formatted it as a plugin. You have to be logged in and an admin to run it. Aug 4, 2012 at 15:21

Use this query to remove thumbnails images and associated metadata from database

global $wpdb;

$attachments = $wpdb->get_results( "
     SELECT * 
     FROM $wpdb->postmeta 
     WHERE meta_key = '_thumbnail_id'
" );

foreach ( $attachments as $attachment ) {
    wp_delete_attachment( $attachment->meta_value, true );

$wpdb->query( "
    DELETE FROM $wpdb->postmeta 
    WHERE meta_key = '_thumbnail_id'
" );

Your Answer

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

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