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' '' --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 ‘/’

Was this post helpful?

Leave a Reply