[resolved] How can I bulk delete all posts in a custom post type? (6 posts)

  1. Nick5a1
    Posted 4 years ago #


    I realise there are a few plugins out there for bulk deleting posts, but none that I have come across work with custom post types. I also can not access the custom post type page in the admin area as it just times out due to too many custom post types (over 15k). I need to delete all these custom posts then do a different import. It was previously advised to me and I tried deleting the following tables in my mySQL database:


    Although this deleted all the custom posts I could no longer post a new post in that custom post type, and consequently could not re-import. I since restored to a previous version of my mySQL database and the tables are intact again.

    Can anyone please suggest how I can delete all the posts of the custom post type without destroying all the extensive customisation that has gone into the custom post type?

    Many thanks.

  2. Nick5a1
    Posted 4 years ago #


  3. Ervald
    Posted 4 years ago #

    Place this to a page:

    // For Custom posts

    // Get 50 custom post types pages, set the number higher if is not slow.
    $mycustomposts = get_pages( array( 'post_type' => 'name-of-post-type', 'number' => 50) );
       foreach( $mycustomposts as $mypost ) {
         // Delete's each post.
         wp_delete_post( $mypost->ID, true);
        // Set to False if you want to send them to Trash.
    // 50 custom post types are being deleted everytime you refresh the page.?>

    // For terms

    $taxonomy = 'my_taxonomy';
    $terms = get_terms($taxonomy);
     $count = count($terms);
     if ( $count > 0 ){
         foreach ( $terms as $term ) {
            wp_delete_term( $term->term_id, $taxonomy );
  4. Nick5a1
    Posted 4 years ago #

    Thanks Ervald this worked perfectly! I was able to change the number to 5,000 and just refresh it 3 times, took a couple minutes each time.

  5. Ervald
    Posted 4 years ago #

    No problem Nick,

    You can mark this post as Solved now I guess.

  6. scott74
    Posted 4 years ago #

    Where exactly to you place this code.. I need to delete a ton of post from a custom post type... Also...Do you need to change anything in the code above..


Topic Closed

This topic has been closed to new replies.

About this Topic