admin管理员组

文章数量:1206200

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?

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?

Share Improve this question edited Aug 21, 2017 at 0:16 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Aug 20, 2017 at 17:34 beeefbeeef 1414 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: performanceIs 30 postssec considered slow for wpdeletepost