How to use “TRUNCATE TABLE” MySQL statement?

I have tried dbDelta and $wpdb->query, they does not work. Does anyone has any ideas?

Related posts

Leave a Reply

3 comments

  1. As try today, truncate is WAY BETTER that DELETE, because DELETE broke the site, and trucate just empty the table, here is the working code :

    global $wpdb;
    
    $wpdb->query('TRUNCATE TABLE wp_posts');
    
  2. I know there are similar answers above but in the above answers, the table wp_post used if by mistake someone uses that code will lose his all posts.

    So sharing a correct way to truncate.

       global $wpdb;
       $table_name = $wpdb->base_prefix.'name_of_table_without_prefix';
       $wpdb->query("TRUNCATE TABLE $table_name");