WP-CLI ยป Use Regex with WP CLI to ๐Ÿ”Ž Search & Replace in Database

Use Regex with WP CLI to ๐Ÿ”Ž Search & Replace in Database

When cleaning websites that have large databases, regular text editors can’t even open the database dumps (Notepad++ supports files up to 2GB in size). So to run a search and replace on large database dumps I suggest using regex-replace line-by-line in PHP, or setting up WPCLI and running regex search & replace.

Here is a quick one-liner WP-CLI command to search & replace in WordPress database using regex.

wp search-replace 'what-to-replace' 'replace-with' --all-tables --dry-run --report-changed-only --precise --regex --regex-delimiter='/'

Here’s a breakdown of what this command does:

–all-tablesrun the search & replace on all WordPress tables in the database
–dry-runonly show what will be changed without actually changing anything
–report-changed-onlyreport changed fields only
–preciseforce the use of PHP (instead of SQL)
–regexsearch using the regular-expression provided
–regex-delimiterset the regular-expression delimiter to ‘/’
See also  Assign all posts from one user to another

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended