60Web60

Managing your database with WP-CLI

WP-CLI2 min read·

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

CommandWhat it does
wp db exportExports the database to a SQL file in the current directory
wp db export backup.sqlExports to a file named backup.sql
wp db import backup.sqlImports a SQL file into the database
wp db checkChecks all database tables for errors
wp db optimizeOptimises database tables to reclaim space and improve performance
wp db repairRepairs corrupted database tables
wp db sizeShows 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-runShows what would be changed without making any changes (always run this first)
wp search-replace 'old.com' 'new.com' --preciseUses 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.

  1. Run the command in dry-run mode first to see what would change:
wp search-replace 'http://oldsite.com' 'https://newsite.com' --dry-run
  1. Review the output. It will show how many replacements would be made in each table.

  2. If the preview looks correct, run it without --dry-run to apply the changes:

wp search-replace 'http://oldsite.com' 'https://newsite.com'
  1. 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