I want to delete all posts from a custom post type "TV-Serie". The number of all posts in this post type are 150,000. Moreover, when I try to delete them all manually while selecting more than 500 posts at once this provides "timeout error".
Any idea on how to delete them all?
I want to delete all posts from a custom post type "TV-Serie". The number of all posts in this post type are 150,000. Moreover, when I try to delete them all manually while selecting more than 500 posts at once this provides "timeout error".
Any idea on how to delete them all?
Few method or approaches you can take to delete all posts from a custom post type.
1 Use WP-CLI (WordPress Command Line Interface)
If you have access to WP-CLI, you can use the following command to delete all posts from your custom post type.
wp post delete $(wp post list --post_type=your_custom_post_type --format=ids)
Replace your_custom_post_type
with the slug of your custom post type. This command will list all post IDs of the specified post type and then delete them one by one
2 Delete Posts Programmatically in Batches
Delete posts programmatically within WP, you can write a custom script to delete posts in batches.
Add code in your themes functions.php
file or in a custom plugin
function delete_posts_in_batches() {
$post_type = 'your_custom_post_type';
$args = array(
'post_type' => $post_type,
'posts_per_page' => 500, // Adjust the number of posts to delete per batch
'fields' => 'ids', // Retrieve only post IDs to improve performance
);
$posts = get_posts($args);
if ($posts) {
foreach ($posts as $post_id) {
wp_delete_post($post_id, true); // Set second parameter to true to force delete
}
}
}
// Call the function to start deleting posts
delete_posts_in_batches();
You can place this code in your theme's functions.php file or in a custom plugin. Adjust the 'posts_per_page' parameter according to your server's capabilities to avoid timeout errors
3 Increase Server Timeout
you can try increasing the maximum execution time and memory limit to prevent timeout errors during the deletion process
DELETE FROM wp_posts WHERE wp_posts.post_type='TV-Serie';
". I'm not convinced that is the best or even a recommended way to do it but it would remove the records (brutally). Seriously, use with the utmost caution; this could go very wrong with the smallest error. – Matthew Brown aka Lord Matt Commented Aug 4, 2019 at 1:47DELETE FROM wp_posts WHERE wp_posts.post_type='episodes';
which provides the following error: "Error in processing request - Error code: 524 Error text: error" – Zulkifl Agha Commented Aug 6, 2019 at 8:41