A migration script that rewrites a meta key on every post runs cleanly in staging — staging has 2,000 posts. Production has 380,000. Run live, it holds a database connection for an hour, blocks editorial saves, and stops halfway when the SSH session drops. Bulk operations on production WordPress need the shape of any batch job: small chunks, idempotent, resumable, throttled.
What’s happening
WP-CLI commands like wp post update and wp option patch run inside a loaded WordPress environment — hooks fire, caches invalidate, drop-ins behave. That convenience is the cost: every iteration boots WordPress, every write triggers save_post, every transient invokes the object cache. Across 300,000 objects, that’s the difference between a 4-minute run and a 6-hour run holding table locks.
The other surprise is implicit pagination. wp post list --post_type=post returns every matching post in one MySQL result set. On a content-heavy site that allocates gigabytes in the PHP process running the CLI.
How to verify
For any candidate batch, first measure the working set:
wp post list --post_type=post --post_status=any --format=count
wp user list --format=count
wp option list --format=count --search='_transient_*'
Then dry-run a slice. wp post update accepts a list of IDs on stdin:
wp post list --post_type=post --posts_per_page=100 --field=ID \
| xargs -n 20 wp post update --post_status=draft --dry-run
--format=ids is the variant designed for piping. For nested option arrays, wp option pluck retrieves a key from a serialized array without round-tripping it through PHP:
wp option pluck siteurl
wp option pluck active_plugins 0
For destructive operations, check the locking situation before kicking off:
wp db query "SHOW PROCESSLIST" | grep -v Sleep
If editors are mid-publish, defer.
The fix
Chunk the work. A working pattern is to page through IDs and pass them to xargs with a fixed batch size, sleeping between batches:
wp post list --post_type=post --field=ID --posts_per_page=-1 \
> /tmp/post-ids.txt
split -l 500 /tmp/post-ids.txt /tmp/post-ids.chunk.
for chunk in /tmp/post-ids.chunk.*; do
xargs wp post update --meta_input='{"migrated_v2":"1"}' < "$chunk"
sleep 2
done
The sleep lets replication catch up and gives editors’ saves a chance to land between chunks. Wrap the loop in a lock file (flock /tmp/migration.lock) so a second invocation can’t run concurrently. Make every write idempotent — skip rows where migrated_v2 is already 1 — so a resumed run doesn’t double-process.
For transient cleanup, prefer wp transient delete --all over hand-written SQL — it respects the object cache drop-in. For SQL you really need to run directly, wp db query pulls credentials and prefix from wp-config.php automatically. wp eval-file is the right tool for anything needing real PHP context; keep those scripts in version control alongside the migration runbook.
Stack Harbor’s enterprise WordPress hosting practice writes these batch jobs as part of every migration and large content change; Deployment Management covers the surrounding release and rollback procedure. If a bulk operation has scared the team off touching production, we can run it for you.