How does one delete orphan custom post type?

I created some posts using a custom post type, then I decided to delete this custom post type but of course the old posts remained orphan inside the database.

How can I remove these orphan posts and all related attachment (post meta, etc.) safely from DB?

Related posts

Leave a Reply

2 comments

  1. DELETE a,b,c FROM wp_posts a
    LEFT JOIN wp_term_relationships b ON (a.ID=b.object_id)
    LEFT JOIN wp_postmeta c ON (a.ID=c.post_id)
    WHERE a.post_type='customposttype'
    
  2. You need to use a few SQL queries, you can run them in something like PHPMyADMIN

    DELETE FROM wp_posts WHERE `post_type` = 'customposttypename'
    DELETE FROM wp_post_meta WHERE `meta_key` = 'metakeyname'
    

    The above two will get you started. You may need to run other queries to clean up other entries but only you will know what other stuff you’ve inserted into your DB.

    A word of warning! MAKE SURE YOU BACKUP YOUR DATABASE FIRST. And also before you run the DELETE query use a SELECT * FROM first to check whats returned and thats what you want to be deleted.