I'm bulk deleting posts from my WordPress site using the function wp_delete_post
and categories using wp_delete_category
. That are about 100.000 posts and 4.000 categories, and after a while I get a PHP Error
PHP Fatal error: Allowed memory size of ... bytes exhausted
That's what I do inside my code:
// get all IDs from the posts and categories
$delete_categories = $wpdb->get_col("SELECT DISTINCT(j.term_id)...");
$delete_posts = $wpdb->get_col("SELECT DISTINCT(j.post_id)...");
$delete_parent_categories = $wpdb->get_col("SELECT tt.parent FROM $tbl_term_taxonomy tt ...");
// I removed the SQL statements for a better reading
foreach ($delete_posts as $dp) {
echo "deleting post ".$dp."\n";
wp_delete_post($dp, true);
unset($dp);
}
unset($delete_posts);
foreach ($delete_categories as $dc) {
echo "deleting category ".$dc."\n";
wp_delete_category($dc);
unset($dc);
}
unset($delete_categories);
foreach ($delete_parent_categories as $dc) {
echo "deleting category ".$dc."\n";
wp_delete_category($dc);
unset($dc);
}
unset($delete_parent_categories);
Deleting the posts works, deleting some categories also works - but at some point the memory limit is exceeded and the script is terminated. Setting the memory limit to 1024MB doesn't look like a perfect solution for me.
Is there a way to free up the memory that the WordPress functions allocated?
header("Refresh:0");