WP-CLI database commands let you export a copy of your database, import a database, run a search and replace across all content, and check the database for errors. The most powerful, and most important to use carefully, is wp search-replace.
Command reference
| Command | What it does |
|---|---|
wp db export | Exports the database to a SQL file in the current directory |
wp db export backup.sql | Exports to a file named backup.sql |
wp db import backup.sql | Imports a SQL file into the database |
wp db check | Checks all database tables for errors |
wp db optimize | Optimises database tables to reclaim space and improve performance |
wp db repair | Repairs corrupted database tables |
wp db size | Shows the total size of the database |
wp search-replace 'old.com' 'new.com' | Replaces all instances of old.com with new.com across the entire database |
wp search-replace 'old.com' 'new.com' --dry-run | Shows what would be changed without making any changes (always run this first) |
wp search-replace 'old.com' 'new.com' --precise | Uses PHP-based replacement for better handling of serialised data |
wp db query 'SELECT COUNT(*) FROM wp_posts' | Runs a raw SQL query (use with caution) |
Using search-replace for a domain change
When you move a site or change its domain, old URLs remain in the database. You can fix them all at once with wp search-replace.
- Run the command in dry-run mode first to see what would change:
wp search-replace 'http://oldsite.com' 'https://newsite.com' --dry-run
-
Review the output. It will show how many replacements would be made in each table.
-
If the preview looks correct, run it without
--dry-runto apply the changes:
wp search-replace 'http://oldsite.com' 'https://newsite.com'
- Flush the cache afterwards:
wp cache flush
Important warnings
The commands wp db drop and wp db reset are destructive and cannot be undone. Always take a backup before using any database command. When using wp search-replace, always test in dry-run mode first.
Need help?
If you have questions about managing your database, visit our support page or email hello@smarthost.ie.
Frequently asked questions
Where does wp db export save the file?
In the current directory of your terminal session, which is your site's root folder. You can then download it using the File Browser in your Web60 dashboard or via SFTP.
Can I use wp search-replace to change my site URL?
Yes. Run wp search-replace 'http://oldsite.com' 'https://newsite.com' --dry-run first to preview, then without --dry-run to apply. Run wp cache flush afterwards.
Is wp search-replace safe for serialised data?
Yes. WP-CLI handles PHP serialised data correctly during search-replace, which avoids the corruption that can happen with a simple SQL REPLACE query.
Last updated: 25 March 2026
