Get my texts in your email

Mass delete WordPress posts – method and performance

A question was raised in a WordPress group on Facebook on what the best way to mass delete all posts in a custom post type was. I suggested using wp_delete_post() within a get_posts() loop.

Since it was Friday evening and and I was curious on how good an idea that was I decided to test it. So I quickly installed a new WordPress installation on my development server, added 3300 pages using Mass Pages/Posts Creator and started doing some tests on that. To benchmark the speeds of the scripts, I took some advice from Stack Overflow and used getrusage(). To get a baseline speed I started with simply making a list of all the the posts:

$args = array( 'posts_per_page' => -1, 'post_type' => 'page', );
$allposts= get_posts( $args );
foreach ( $allposts as $post ) :?>


	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

<?php endforeach; 

Started with running that once on each page load, then doing multiple runs on the same load. When trying to test the limits I accidentally set the code to repeat 1000 times when I was going to write 100 – I gave up on that pageload after a few minutes :).
After getting the baseline values for the speed I tried using wp_delete_post() in the loop:

$args = array( 'posts_per_page' => -1, 'post_type' => 'page', );
$allposts= get_posts( $args );
foreach ( $allposts as $post ) :
	wp_delete_post( $post->ID, true );

After repeating this process several times I decided to get some more data by running the tests on a SiteGround shared hosting account I also have.

The final execution times I measured:


Listing 3300 posts: ~250 ms
Deleting 3300 posts: ~3200 ms

SiteGround shared hosting:

Listing 3300 posts: ~1200 ms
Deleting 3300 posts: ~5600 ms

I learnt a lot and had a lot of fun. Hopefully this write up can be useful for someone else as well. If you stumble here and have no clue how to use any of this code – post a comment with what you’re trying to achieve and I’ll try to help you.

4 thoughts on “Mass delete WordPress posts – method and performance

  1. Good article Lars.

    I’m currently looking if it’s possible to pass an array of IDs to the wp_delete_post function, so we don’t have to run it every time per post. One mass delete.

    Something probably considered by others already, but checking anyway.

Leave a Reply

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