Delete unattached media in WordPress in a few seconds

permanently

When you delete your posts, attachments from it don’t get deleted automatically. They keep staying in the WordPress Media Library and on your web server eating disk space and messing the things up.

Delete unattached attachments (orphaned media that is not attached to any WordPress post, page or custom post)

This function will delete them permanently from Media Library and from wp-content/uploads folder too.

Delete unattached media in WordPress in a few seconds

Just mind what your are doing.

On the one of the projects we added images and descriptions on tags archive pages for SEO purposes and these images as you understand are not attached to any post, however that doesn’t mean we want to get rid of them.

If you’ve thoroughly thought this out and you are  sure you want to get rid of this orphaned media, make backup and just do it 😉

Delete custom post type attachments

This function is useful when you need to delete all images that are attached to specified CPT.  Mind to change “card” to your custom post type name.

You’ve made a backup, have not you?)

4 Replies to “Delete unattached media in WordPress in a few seconds”

    1. hey,
      All functions can be placed in functions.php file of the current theme or in the plugin and fired with actions like «init», «wp_head» etc or run with shortcode depending on your goals. Good luck!

  1. It doesn’t work for me. I copied & paste the code into functions.php of the Twenty Seventeen theme right at the very bottom. I refresh my site but I don’t see it deleting unattachments

  2. Hi, I just came across your post here about deleting orphaned media. It looks great and just what I need. However I’m a little unsure just what to do with your code. Can you explain a little on what to do with it (for a relative newby)
    Many many thanks.
    Regards
    Wayne

Leave a Reply

Your email address will not be published. Required fields are marked *