In WordPress, if you need to delete a particular published post and all its related contents (tags, comments, meta data, …) you have to move it to the Trash folder first. After that, you have to go to Trash folder to delete that post. WordPress allows you to do that with just few clicks by choosing multiple posts in one time to Move to Trash and Delete Permanently It’s pretty cool, isn’t it?
However, if you’re going to delete thousands of dump/out of date posts, it could be a problem. You have to do the same process again and again in few days even a whole week.
Today, I’ll show How I completely delete a Bulk of posts (12000) in 5 minutes. The process could be faster or slower depend on your server performance. You is required to have a little bit knowledge on PHP Programming and have FTP log in detail to upload your file.
Let create a new file (ex: _delete_posts.php) in your WordPress web root folder and replace its content with the source code below, it will permanently delete 12000 posts in drafts folder or you can change post_status to trash to delete all posts in Trash folder.
<?php set_time_limit(600); require_once('wp-load.php'); wp(); $sql_get_posts = "select wp_posts.ID FROM wp_posts WHERE wp_posts.post_type = 'post' AND (wp_posts.post_status = 'draft') ORDER BY wp_posts.post_modified DESC LIMIT 0, 12000"; $posts = $wpdb->get_results( $sql_get_posts ); foreach ($posts as $post) { wp_delete_post( $post->ID, true ); echo "Deleted post id: " . $post->ID . "<br>"; } echo "OK"; exit(); ?> |
Note:
- 1. Replace the ‘wp_’ with your table prefix in wp-config.php file
- 2. Modify the SQL Command (sql_get_posts) in order to filter post IDs you want to delete
- 3. Understand which SQL commands will access to your database by Monitoring all MySQL activities
- 4. Just run the script on Production server after you Double check on development environment.
- 5. Backup your database
- 6. Run Optimize Table command for affected tables after you delete bulk of posts.
Post comment if you have any question, I’m here to share and help 🙂
Good luck 🙂