As I understand default WP behavior:

When you upload an image to a post and save it (as a draft or published post), the image becomes "attached" to the post. This can be double checked by going to Media > Library > 'Uploaded To' column.

In order to mark an image as unattached to any post, it must be done manually by deleting the post. (Is there another way?)

My main question is: Is there a way for me to tell WordPress to mark images as "unattached" if they are not inserted or linked in any existing posts?


To clarify:

  1. We upload 20 (or more) images to a post and insert them. These are all automatically attached by WordPress to the post. We want this to happen, because our theme template pulls all attached images per post.

  2. Before the post is published, it's edited and some images are taken out and won't be used again.

  3. We're looking for a way to detect which images are not being used* in the post, and automatically unattach them from that post. This is so that we can just head into Library > Unattached, select them all and delete them.

* I originally used the term 'unlinked' because all the inserted images are <a href=""><img></a> so it may help in terms of detecting what are being used because a link to the image is present in the post.

  • can't you upload the images using the Media -> Add New menu? This way they'll never be attached.
    – moraleida
    Aug 6, 2014 at 1:10
  • @moraleida There's a set of 20 or more images that are all uploaded to a post. We want the default behavior to automatically attach them all. But after the editing, we'd like a quick way of detecting (if ever there are) which ones aren't being used in the final post and quickly delete them.
    – Arkuen
    Aug 6, 2014 at 1:12
  • have you seen this: wordpress.stackexchange.com/a/88104/7890 ? Should get you going.
    – moraleida
    Aug 6, 2014 at 1:30
  • Can you explain what exactly bothers you with the images being attached to a post? I don't think you can unattach them from the admin but unless you use the gallery shortcode in the default way I don't see the harm in them being attached. Aug 6, 2014 at 2:38
  • It sounds like what you're asking for in your above comment, and what you're asking for in your post are two different things. I think this might just be a matter of clearing up definitions. Any time an image is uploaded in the new/edit post screen it becomes "attached" to that post, meaning it's a child of that post, whether it is actually used or not. According to your comment, that isn't the problem -- you want to know which images aren't in use. Is that correct? Aug 6, 2014 at 2:42

2 Answers 2


wordpress sucks in keeping media<=>content relationships. Part of the problem is that by default all media are public once they are uploaded and you have no way to know where are they are being used. Just because an image is not referenced anymore in its original post doesn't mean that it is not referenced at any other place (sidebar widget?) or maybe by some external site, therefor deleting an image just because it is unattached is a very dangerous idea that can lead to a very big fail situation.

For what you want I think it is better to use the actual server access log than the content. Get a process running that once a month goes over all the images and find those that didn't have any hits, and go and delete them.


A bit of background to make sure we are in sync:

  • Uploading an image means that you create a new post (of type attachment) in WordPress database and you create a few image files in WordPress file system (one file per size)
  • Attaching to a post means that you mark the (hereabove) attachment post as child of this post. Meaning that an attachment can be only attached to one post.
  • Inserting an image in a post only means you add an img element to the post. The source (attribute src) points to the url of the file created when you uploaded the image.

Elements of answer:

  1. There is no built-in way to know which images are inserted. You have to parse the content of the post.
  2. You cannot undo the parent/child relationship between the post and the attached image unless you delete the post or the image

What I woud do to remove useless images from a post:

  1. Edit post
  2. Remove the img element from the content
  3. Click on 'Add Media'
  4. Select 'Uploaded to this post' in drop down menu
  5. Select the media
  6. Click on 'delete permanently'

This will work only if your policy is to use attachments only in one and only one post.

  • You've hit the nail on the head. What you are suggesting (steps 1-6) is what we are currently doing. What I'm wondering is how difficult/possible it would be to create a function that can "parse the content of the post", match inserted images (maybe an array of URLs from content?) against an array of URLs from the library and delete those that don't have a match
    – Arkuen
    Aug 6, 2014 at 10:45
  • Quick and dirty: you could look for pattern 'wp-image-xxx' where xxx is the post id of the attachment you want to keep.
    – YaFred
    Aug 6, 2014 at 12:31
  • How could I go about doing that? I've never actually hooked into WordPress core before. (I assume that's what's needed in order to replicate the 'delete this image' function in Media Library)
    – Arkuen
    Aug 7, 2014 at 0:26

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.