I have about 90,000 posts in my WordPress and want to delete most of them using a plugin. I fetch all IDs from the posts that need to be deleted and then do the following:

// $delete_posts contains all IDs
foreach ($delete_posts as $dp) {
    wp_delete_post($dp, true);

I'm sitting here wondering why my plugin doesn't work as expected - I checked my database with PhpMyAdmin and realized that the posts are deleted very very slow. Maybe 30 posts per second? That's definitely too slow. Or am I misusing this function to delete posts?

  • Did you deactivate themes and plugins and try again? Even then, deleting posts isn't an easy tasks, as the doc states "When the post and page is permanently deleted, everything that is tied to it is deleted also. This includes comments, post meta fields, and terms associated with the post." - and because of the modularity of WordPress, everything is done separately instead of using a single query for all. – kero Aug 20 '17 at 17:55
  • If by slow is meant a few minutes then it is, but 1 hour to delete all your post (30x60x60=108000) is not that bad. As explained by Jack in his answer it takes some to do it save and secure. Add extra memory for database use (on the server/edit specific db conf files also) and you could speed it up. – Charles Aug 21 '17 at 15:53

Speed factor of any function that deals with content such as wp_insert_post() or wp_delete_post() highly depends on the situation and the content that it's dealing with.

Let's make a simple example. You have a post, that contains featured image, dozen of tags, categories, a thousand custom fields, assigned taxonomies, and a hundred more. When you run a command to delete this post, you are not just trying to delete a permalink from the database, you are also trying these:

  • Detach the post thumbnail
  • Remove a dozen of term relationships
  • Run a loop and deal with a million of metadata
  • And possibly more.

If you run a SQL command and just remove the post's data from the database (not recommended), it might speed up to thousands of posts per second. But, fully removing the content and unlinking the relationships is heavy.

  • I store all the post IDs in a separate table in my database. What if I just remove all posts from the wp_post table, wp_postmeta and wp_term_relationships? I think thats a lot faster and should remove all information that's correlated to each post. – beeef Aug 27 '17 at 13:42

Yes, definately 30 secs is too much for deleting the post. But seems, the problem isn't it wordpress - it could be even another framework either core PHP. In my opinion, the problem with database mysql and its optimization. Just check how many records do you have, how is a primary key (do you delete by ID or post name?), how many columns etc. What is important is to know that that table opens fast. For instance, in our largest project with 3M records, they deleted quite quickly - http://merehead.com/projects/frevend/

  • He's talking about 30 posts PER second :) – gordie Jan 9 '20 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.