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 ) :?>

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

<?php endforeach; 
wp_reset_postdata();

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 );
endforeach; 
wp_reset_postdata();

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:

VPS:

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.

Expect nothing

It’s easy to forget how great your life is when you are occupied by the pursuit of some far of vision away what your life could be. Whether you are reaching for the fame and fortune of the stars or succumbing to the pressure of what great things you feel lay within your destiny your current life decays while you chase a illusion of a life where you imagine yourself happy.

Continue striving towards greatness, but don’t base your happiness on how successful you are. Expect nothing, practice poverty, slow down, keep a gratitude journal. Look at stars, pet a cat, kiss a loved one.

Set aside a certain number of days, during which you shall be content with the scantiest and cheapest fare, with coarse and rough dress, saying to yourself the while: “Is this the condition that I feared?” – Seneca

Wallet

My wallet if full of things.

Four dollars.
Forty five euro.
Three hundred and twenty kronor.
Fifteen yuan.
A hastily written phone number from a girl in Shanghai.
The autograph of an artist from Köln.
A polaroid from adventures in Vienna.
A receipt for a cup of tea from a temple in Hangzhou.

But most of all my wallet is filled with memories.

Listen to the losers

No matter if we are talking about Trump supporters, Sweden Democrats or Britons voting for leaving the EU1 the core nationalistic right wing populists are losers2. Not in the sense that they are bad people. Instead they feel like the current direction society is taking is leaving them and their peers behind. Societal change is scary and they can’t keep up.

Gays are marrying while they can’t be in a happy relationship, muslims are working while they are unemployed or otherwise miserable with their job situation and they think immigrants get free money while they live paycheck to paycheck. They look at an idealization of the privileges their parents had, privileges that are slowly drifting away from them. They conclude that the ones in charge are not addressing their problems, choosing others over them. They are working class. Not used to being on top of the societal ladder, but at least they have had some sense of privilege as white males. Rightly or wrongly they feel that what status they have had in society is slipping away. All around them other disenfranchised groups are rising in status and getting help from society, and because they wrongly look at life as a zero sum game they feel this is at their expense.

When you feel like you are a fringe part of society and no one is listening to you it’s easy to look for something to blame and to look for an easy solution to your problems. Democracy is obviously not working (For you), so a strong ruler with clear goals and an easily definable “other” is a very strong proposition. They are further egged on by a media that’s horrible at portraying what’s actually happening in the world3. A media that’s simultaneously (and often times misguidedly) trying to cover up the background on non native criminals while still managing to make muslim terrorism look like a major risk and making people believe there are forteen times as many muslims in the country as there really are 4.

While I vehemently believe that we should do all possible to hold the extreme right wing parties at bay, I think we also have to listen to their voters. Try empathising with what they are feeling and talk to them, instead of ostracising them because they are afraid for their future. Calling people who disagree with you “hillbillys”, “stupid”, “nazis” or in other ways further disenfranchising them will only further the divide and make them even more sure in their view of the world. Change is scary, and society is quickly morphing into something other than the rose coloured vision they have of the perfect society of yore. Hating them only makes them hate more, even if your sympathy is not enough to teach them to love, maybe it’s enough to help them accept and adapt.

  1. I hate the term “Brexit”, even worse than that is “Bremain”. Stop using tabloid headlines for serious issues.
  2. I’m trying to describe the core voters, in Sweden a lot of voters are turning to the Sweden democrats because they are afraid that the current levels of immigration will be too costly for the Swedish public institutions to remain effective. Other parties are quickly running towards limiting immigration, either because they believe that assessment or because they want to stop their voters from leaving for the Sweden Democrats instead. There are probably a bunch of people voting for Britain leaving the EU that has other reasons than the ones described as well.
  3. Something that’s also paving the way for “alternative non-pc news” that make the astonishing thing of actually making and even worse job of informing people on what happens in the world.
  4. Those figures are for the US. Swedes only believe there are about 2.5 times as many muslims as there really are (https://www.theguardian.com/news/datablog/2014/oct/29/todays-key-fact-you-are-probably-wrong-about-almost-everything).

This post has been slightly edited on November 9th 2016 to reflect the American Presidential Election.

Make the world better

Be kind.

Don’t succumb to only helping people based on what value you perceive they can reciprocate in the future. Instead be generous with your knowledge and your time because that’s the kind of person you want to be and the kind of world you want to contribute to. I think that makes you happier and the world a more magical place.

“Carry out a random act of kindness, with no expectation of reward, safe in the knowledge that one day someone might do the same for you.” -Princess Diana

Breathe in

Close your eyes.

Breathe in counting to six, hold counting to six, breathe out counting to six.

Breathe in.
Hold.
Breathe out.

Feel the breath rejuvenating your body, awakening parts of you that you did not know existed.

Notice your breathing, feel the air passing through your nose and down into your loungs. Take note of the feeling of your chest rising and falling. Imagine a ball of energy glowing within your chest. Take good care to not allow it to burn you. Extinguish it quickly.

Start scanning your body, starting with the top of your head, slowly feeling the sensations of your body until you reach your toes.

Don’t get startled when you feel a cold breath running down your neck. Just carry on and hope he won’t hurt you this time.

Take some extra time to take in the feeling of your body pushing towards the altar. Feel the strange warmth of the stone underneath you.

Now it’s time to reawaken your body and mind. Keeping your eyes closed, notice the sounds around you. Feel the room around you shrink. Feel your clothes ripping as your wings expand from your back. Wiggle your fingers and tentacles.

Open your eyes. Sit for a few moments and enjoy how relaxed you feel and appreciate your body being reawakened and your mind starting to return to it’s usual level of alertness.

Slowly stand up, and continue with the rest of your day, full of energy.