This is a follow-up of my question which did solve my problem, well, a little bit.

I noticed that there are still images on my uploads folder but of different size. Is there a way to delete all traces of the image that I uploaded? It seems that wp_delete_attachment only deletes the image that is "attached" but not the other image sizes that Wordpress automatically created.

I use this function to generate attachment IDs

function insert_attachment( $file_handler, $post_id, $setthumb='false' ) {

    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attach_id = media_handle_upload( $file_handler, $post_id );

    if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);

    return $attach_id;  

(I got that from: http://goldenapplesdesign.com/2010/07/03/front-end-file-uploads-in-wordpress/)

I use this for a custom file upload in front-end and not the usual upload using the Media Library. This is for a site that requires user uploads.

Any thoughts?

  • 1
    It should delete all of them. Can you see if something is interferring? It uses get_intermediate_image_sizes() to determine which sizes are supposed to be deleted. Did you change the default wordpress naming pattern or directory structure? Are you using some code to dynamically generate some of the sizes? Commented Nov 7, 2012 at 18:51
  • @MridulAggarwal I am not really sure about that. I will chechk again if something was interfering but I am sure that I didn't change the naming pattern nor the directory structure. Though I noticed that even though I uploaded the image now, it seems to get uploaded to the folder created last september (09 folder). Also, I did not use codes to dynamically generate images. I will check again and update you. Thanks
    – mcometa
    Commented Nov 8, 2012 at 5:44
  • If one of the extra images is in a seperate folder from the original image, then wordpress won't find it. Could this be the problem? Commented Nov 8, 2012 at 7:20
  • @MridulAggarwal I edited my question and added the code that generates the attachment ID. Maybe you can help figure it out. Thank ouy very much.
    – mcometa
    Commented Nov 8, 2012 at 22:38
  • Not the solution but in place of update_post_meta you should use set_post_thumbnail() Commented Nov 9, 2012 at 8:46

1 Answer 1


This can be achieved with wp_delete_attachment( $attachment->ID );

Here's an example to delete all attachments related to a post:

// Delete Attachments from Post ID 25
$attachments = get_posts(
        'post_type'      => 'attachment',
        'posts_per_page' => -1,
        'post_status'    => 'any',
        'post_parent'    => 25,
foreach ( $attachments as $attachment ) {
    wp_delete_attachment( $attachment->ID );

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.