How to update WordPress with WP-CLI and roll back if it breaks
Security releases for WordPress core land with little warning, and the safest time to apply one is right away. The risk is the update itself: a plugin that breaks on the new version, a database upgrade you cannot undo, or a half-finished unpack. This guide takes a rollback point first, updates core with WP-CLI, checks the site, and shows the exact commands to put the old version and the old database back if anything is wrong.
What to back up before you update
A core update replaces WordPress's own files. A major update can also change the database schema, which is why WP-CLI has a separate wp core update-db step. Plugins, themes and uploads in wp-content are not replaced by a core update, but your rollback point should still cover the whole site so you can recover from anything.
- A provider snapshot of the server, for the case where the server will not come back at all. See Hetzner snapshots or DigitalOcean snapshots.
- A database dump taken with
wp db exportjust before the update. - An archive of the site files, including
wp-contentandwp-config.php. - The version number you are running now, so you can reinstall exactly that version.
The commands below are for Debian or Ubuntu with WordPress in /var/www/html, files owned by www-data, and WP-CLI installed as wp. Change the path and user to match your server. WP-CLI should run as the user that owns the files, not as root.
Check what you are running
cd /var/www/html
sudo -u www-data wp core version --extra
sudo -u www-data wp core check-updatewp core version --extra prints the WordPress version and the Database revision. Write both down. wp core check-update lists the versions available to you. If you only want the latest release in your current branch, use wp core update --minor in the next steps instead of wp core update.
Take the backups
sudo mkdir -p /var/backups/wordpress
sudo chown www-data:www-data /var/backups/wordpress
cd /var/www/html
sudo -u www-data wp db export /var/backups/wordpress/pre-update-$(date +%F).sql
sudo tar -czf /var/backups/wordpress/files-pre-update-$(date +%F).tar.gz -C /var/www htmlwp db export reads the database name and credentials from wp-config.php, so you do not need to type a password. The tar archive holds the whole web root, including wp-content and wp-config.php.
A dump and a snapshot that only exist on the same server do not help if that server is lost or compromised. Copy the two files to storage you control somewhere else before you start, for example a bucket on Backblaze B2 or Cloudflare R2.
Run the update
cd /var/www/html
sudo -u www-data wp core update
sudo -u www-data wp core update-db
sudo -u www-data wp core verify-checksumswp core updatedownloads and unpacks the latest release. Add--minorto stay on your current branch.wp core update-dbruns the database upgrade. If nothing needs to change, it says so.wp core verify-checksumscompares every core file with the official checksums for the installed version. You want Success: WordPress installation verifies against checksums.
If the update stops with Another update is currently in progress, make sure no other update really is running, then clear the lock with sudo -u www-data wp option delete core_updater.lock and run the update again.
Check the site
sudo -u www-data wp core version --extra
curl -sI https://<your-domain>/ | head -n 1
curl -sI https://<your-domain>/wp-login.php | head -n 1You want the new version number and 200 responses. Then log in to wp-admin, open a few pages that use your main plugins, and watch your PHP error log while you do. Compare the Database revision with the number you wrote down: if it changed, the update modified the database, and a rollback must restore the dump as well as the files.
Roll back if it breaks
Replace <previous-version> with the version you wrote down and <date> with the date in your dump's file name.
cd /var/www/html
sudo -u www-data wp maintenance-mode activate
sudo -u www-data wp core update --version=<previous-version> --force
sudo -u www-data wp db import /var/backups/wordpress/pre-update-<date>.sql
sudo -u www-data wp core verify-checksums --version=<previous-version>
sudo -u www-data wp maintenance-mode deactivate- Maintenance mode shows visitors a short notice while you work.
wp core update --version=<previous-version> --forceinstalls the older release.--forceis required because the installed version is newer than the one you ask for.wp db importloads the pre-update dump, which puts the database revision back to what the old core expects.wp core verify-checksums --version=<previous-version>confirms the core files now match the old release.
Importing the dump replaces the whole database. Posts, comments, orders and form entries created after the dump was taken are lost. On a busy shop, export anything new first, or roll back only the files if the database revision did not change.
If the server will not boot or PHP will not run at all, restore the provider snapshot instead, then copy across anything that changed since. Restore a server from backup walks through that.
Schedule nightly database backups
A dump taken before each update is the minimum. A nightly dump means a bad update, a bad plugin or a mistake in wp-admin never costs more than a day. Find the full path to WP-CLI with command -v wp, then create this file.
# m h dom mon dow user command
30 2 * * * www-data cd /var/www/html && /usr/local/bin/wp db export /var/backups/wordpress/db-$(date +\%F).sql --quiet
45 2 * * * root find /var/backups/wordpress -name 'db-*.sql' -mtime +14 -deleteThe % signs are escaped because cron treats a bare % as a newline. The second line keeps 14 days of dumps on the server. For more on cron schedules, see the cron backup schedule guide.
Dumps that only live in /var/backups disappear with the server. Copy them offsite every night as well, and keep the files archive on a schedule too: uploads change every day.
Prove the backup restores
Load a dump into a scratch database and count the tables. On Debian and Ubuntu, MariaDB and MySQL let the system root user in through sudo mysql by default.
sudo mysql -e "CREATE DATABASE wp_restore_test"
sudo mysql wp_restore_test < /var/backups/wordpress/db-<date>.sql
sudo mysql -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'wp_restore_test'"
sudo mysql -e "DROP DATABASE wp_restore_test"
tar -tzf /var/backups/wordpress/files-pre-update-<date>.tar.gz | headThe table count should match your live database. The tar -tzf listing shows the archive opens and contains html/wp-config.php and html/wp-content/. For a full rehearsal on a second server, see How to test a backup restore.
Frequently asked questions
- Can I downgrade WordPress without restoring the database?
- Only if the database revision did not change. Run
wp core version --extrabefore and after the update. If the revision is the same,wp core update --version=<previous-version> --forcealone is enough. If it changed, import the pre-update dump as well. - Does wp core update also update plugins and themes?
- No. It only replaces WordPress core. Plugins and themes are updated separately with
wp plugin updateandwp theme update, so back up before those too. - Why run WP-CLI as www-data instead of root?
- Files written during the update keep the right owner, so WordPress can still write to them afterwards. WP-CLI also refuses to run as root unless you add
--allow-root. - Is a provider snapshot enough on its own?
- It is a good rollback point for the whole server, but restoring it rolls back everything on the server to that moment. A database dump and a files archive let you roll back only WordPress, and they can be stored with another provider.
How this was checked
Commands, limits and prices were checked against these official pages, on November 9, 2026: