How to Bulk Delete All WordPress Posts (with code)

Sometimes you have to delete all your post to start from the beginning. It’s very easy to delete all posts if you have limited posts, but if you have a big post list like 50k, it will take so much time if you delete manually. You can set ‘Number of items per page’ to 999 on ‘Screen Options‘ and delete manually but it will require so much manual work. So in this tutorial, we will learn 2 easy ways to delete with code.

Method 1 – WP CLI

With WordPress CLI, and using wp post delete command We can safely delete all the posts we want. Here is the code:

wp post delete $(wp post list --post_type='post' --format=ids)

You can change --post_type to any custom post type. Also if you face any problem like ‘Argument List Too Long’ in wp-cli, please reduce the post number by --posts_per_page=20000 with your desired number so the full code will be like

wp post delete $(wp post list --post_type='post' --posts_per_page=20000 --format=ids)

Method 2 – WP Query

We can delete all the posts by generating WordPress query also. With get_posts we will get all the posts list then we will delete the post by ID with wp_delete_post() function. Here is the code:

$args = array{
    'post_type' => 'post', //you can change to any custom post type
    'numberposts' => -1, //any number, or -1 means all posts
}
$post_lists = get_posts( $args );

foreach ($post_lists as $single_post) {
    wp_delete_post( $single_post->ID, true );
}

Add the code to your header.php or footer.php and run your website, and all of your posts will be deleted. Then remove the code from header.php or footer.php