WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having a trouble un-attaching media from a post without deleting it entirely from my site. Is there a simple way to unattach an image from a post for instance?

share|improve this question
up vote 2 down vote accepted

I learned from Brian Fegter that you can Un-attach an image from a post while participating in this question: Can I attach an image to a different post?

It isn't exactly "simple", but not to difficult. It's the only solution I've been able to find.

share|improve this answer

Is this what you're looking for?

e.g. Remove all image attachments from a post

//get all image attachments
$attachments = get_children( 
                        'post_parent'       => $post->ID,
                        'post_mime_type'    => 'image',
                        'post_type'         => 'attachment'

//loop through the array
if( !empty( $attachments ) ){   
    foreach( $attachments as $attachment ){
        // Update the post into the database
          wp_update_post( array(
                    'ID' => $attachment->ID,
                    'post_parent' => 0

However, please take note of the caution when using wp_update_post.

Alternate method using $wpdb

//replace this with the above inside the foreach block;
global $wpdb;
    UPDATE $wpdb->posts 
    SET post_parent = 0
    WHERE ID = $attachment->id
        AND post_type = 'attachment'
share|improve this answer

As of WordPress 4.2 this functionality is built in. Click the Detach link in media list view.

For older versions, there's a plugin which does the same thing: https://wordpress.org/plugins/unattach/

Unattach is a very simple plugin which allows detaching images and other media from WordPress posts, pages and other content types.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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