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?)

2 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!

Leave a Reply

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