1

I have a function that sideloads a YouTube thumbnail onto a page... but I want to remove the featured thumb on the page first (and remove all the various auto-generated sizes, too).

I'm assuming I want to use delete_post_thumbnail() followed by wp_delete_attachment() to un-associated the thumb from the post, and then kill the thumb file. Is that correct? And Will that also delete all the various thumb media files (the extra auto-generated sizes) from the disk?

4

Using wp_delete_attachment( $attachmentid, true ) is only thing you need.

Passing true as second argument, ($force_delete see codex ) it:

  • remove the thumbnail association with any post
  • delete any taxonomy associated to attachment
  • and of course remove all the files, also the autogenerated ones

So you do not need to also use delete_post_thumbnail().

  • 1
    Thanks! I have a post ID, so I'm just using: $existing = get_post_thumbnail_id( $post->ID ); if($existing) wp_delete_attachment($existing, true); – Eric Aug 28 '13 at 20:40
  • 3
    @Eric you do not need the check. wp_delete_attachment(get_post_thumbnail_id($post->ID), true); is enough. If there is no thumbnail get_post_thumbnail_id returns an empty string, and passing an empty string to wp_delete_attachment as first argument it does nothing and returns an empty string too. – gmazzap Aug 28 '13 at 20:44

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.