I need your help. I am trying to mass delete attachments from a custom post type ('book') using a sql query. I found this snippet on the wordpress form: Wordpress.org

I tested it on my local computer, and it removes all of the attachments from my media library. Now, I would like to have your help in setting the query so that it will only target attachments(featured image and thumbnail) from my custom post type 'book'.


  • is post_type also equal to book?
    – eyoung100
    Commented Jun 27, 2014 at 19:55
  • @ECarterYoung, Yes.
    – user51781
    Commented Jun 27, 2014 at 20:03

2 Answers 2


You can do it with native WordPress codex calls like this ( source http://badlywired.com/technical-stuff/2014/11/10/code-to-delete-all-images-from-wordpress-or-nearly-all/ )

/*  create this code in a file in the main wordpress directory e.g. delmedia.php
and access it via mydomain.com/delmedia.php
// Include the wp-load'er
$args= array(
'post_type'      => 'book', // obvious
'posts_per_page' => -1            // get them all 

// get all attachments post ids
$posts = get_posts( $args );
foreach ($posts as $post_id) {
   // get an array of image data
   $image_attributes = wp_get_attachment_image_src( $post_id->ID );
   if (strpos($image_attributes[0], 'mystring') !== FALSE){
   echo 'Image Found : '.$image_attributes[0];
   if (false === wp_delete_attachment( wp_delete_attachment( $post_id->ID, true     ) ) ) {
       echo ' and delete failed!<br>';
     } else {
       echo ' and delete succeeded!<br>';

Combining two methods here:

$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post_id

$attachments = get_posts($args);
if ($attachments) {
DELETE FROM wp_postmeta
WHERE post_id IN
FROM wp_posts
    WHERE (post_type = 'book')


DELETE FROM wp_posts WHERE (post_type = 'book')

This should give you post types that are attachments and books combined...

  • OMG! So Simple.. I tried so many different variants, that is probably the only one I did not try. I will give it a try right now. Thanks for your time and effort.
    – user51781
    Commented Jun 27, 2014 at 20:14
  • @E Carter Young, Unfortunately that did not do not work. The first query threw an error, so I changed it to this: DELETE FROM wp_postmeta WHERE post_id IN ( SELECT id FROM wp_posts WHERE (post_type = 'attachment') AND post_type = 'book' ); . For the second Query is not running no matter what I try.
    – user51781
    Commented Jun 27, 2014 at 20:33
  • The final query is still not running.
    – user51781
    Commented Jun 27, 2014 at 20:39
  • 1
    This query doesn't make sense, post cannot simultaneously have two different post types.
    – Rarst
    Commented Jun 27, 2014 at 20:49
  • @Rarst That's why I asked him if book was a post type... user is book at category or a post type?
    – eyoung100
    Commented Jun 27, 2014 at 21:00

Your Answer

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

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