When performing intensive or long-running operations on a WordPress website, the admin panel is terrible. Have you tried to delete 100 posts from the posts screen? It’ll time out and delete maybe 10-20 if you’re lucky. This is one example of many.
Naturally, I opted for the WP CLI (WordPress CLI), which allows you to perform operations on your WordPress site from the terminal. I needed to delete custom post types from within a date range in my use case.
At first, I ran the command, which looked like this:
wp post delete $(wp post list --post_type='news' --posts_per_page=25000 --format=ids --meta_key="story_promoted_from_ingest" --meta_value="0" --start_date=2020-01-01 --end_date=2022-03-31) --force
This worked, but the performance was terrible. I was averaging maybe 20 posts per minute. It was taking forever to delete these. Not able to find a solution quickly, I deep-dived the WP CLI docs and found a heap of flags you can pass to it, most notably
What contributed to the slowness was some plugins triggering filters and actions as the posts were deleted.
wp post delete $(wp post list --post_type='news' --posts_per_page=65000 --format=ids --meta_key="story_promoted_from_ingest" --meta_value="0" --start_date=2020-01-01 --end_date=2022-03-31) --force --skip-plugins --skip-themes
By adding those two flags, it was a night and day difference. I was averaging close to 60 per minute (sometimes more). Skipping plugins and themes allows WP CLI to shed weight and run faster. Problem solved.