1

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?

2
  • 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
    Commented Aug 20, 2017 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
    Commented Aug 21, 2017 at 15:53

1 Answer 1

5

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.

1
  • 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
    Commented Aug 27, 2017 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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