WordPress WP-CLI 2 cheatsheet

WordPress WP-CLI 2: Database

7 common WordPress WP-CLI 2 tasks.

Back up the database

Write the whole database to a SQL file.

wp db export backup.sql

Restore the database

Import a SQL file into the database.

wp db import backup.sql

Caution: Overwrites tables with the same names. Export a backup first.

Preview a URL change

Show how many replacements a search and replace would make, without changing anything.

wp search-replace 'http://old.example' 'https://new.example' --dry-run

Change a URL everywhere

Replace a URL across the database. It handles serialized data safely, unlike a plain SQL replace.

wp search-replace 'http://old.example' 'https://new.example' --skip-columns=guid

Caution: Export a backup first, and run the --dry-run version to check the count.

Check the database

Run a check on the database tables.

wp db check

Optimize the database

Run optimize on the database tables.

wp db optimize

Empty the database

Delete all tables in the database.

wp db reset --yes

Caution: Deletes every table and all content. --yes skips the confirmation prompt.

Was this page helpful?