WP-CLI: Managing WordPress from the Command Line

Published: March 30, 2026 · Author: Marcin Szewczyk-Wilgan

The WordPress admin panel works fine when you manage a single site. With multiple sites, staging environments, regular updates, and cron tasks – clicking through the interface becomes a bottleneck. WP-CLI is the official command-line tool for managing WordPress. It lets you do everything from the terminal that you do in the admin panel – and much more: bulk plugin updates, database export and import, user management, transient cleanup, cron management, post-migration search-replace, and automation of repetitive tasks in bash scripts. If you are looking for lower-level commands – grep, find, sed, awk – check out the article WordPress on the Server Terminal. In this article, you will find practical WP-CLI commands grouped by topic – from installation and configuration, through daily maintenance, to advanced database operations and automation.

1. Installation and configuration of WP-CLI

WP-CLI requires PHP 5.6+ and terminal access on the server. Most good hosting providers (VPS, dedicated, quality shared) have WP-CLI pre-installed. If not – installation takes a minute. Need help with environment setup? We handle server administration and WordPress hosting.

Install WP-CLI:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp

Check version and environment:

wp --version wp --info

Update WP-CLI to the latest version:

wp cli update

Command auto-completion (bash completion):

wp cli completions --type=bash >> ~/.bashrc source ~/.bashrc

Run as a different user – important on servers with multiple sites:

sudo -u www-data wp option get siteurl --path=/var/www/html

2. WordPress installation and management

WP-CLI lets you install WordPress from scratch – downloading files, creating wp-config.php, installing the database – without opening a browser. Useful for deployment automation and server provisioning.

Download WordPress files:

wp core download --locale=en_US

Create wp-config.php:

wp config create --dbname=wordpress --dbuser=root --dbpass=password --dbhost=localhost --dbprefix=wp_

Install WordPress – create database and admin account:

wp core install --url="https://domain.com" --title="My Site" --admin_user=admin --admin_email=admin@domain.com --admin_password=strongpassword

Check the current WordPress version:

wp core version

Update WordPress to the latest version:

wp core update wp core update-db

Toggle debug mode:

wp config set WP_DEBUG true --raw wp config set WP_DEBUG_LOG true --raw

Disable debug mode after diagnostics:

wp config set WP_DEBUG false --raw

3. Plugins and themes

Managing plugins and themes with WP-CLI is a clear advantage over the admin panel – especially for bulk updates, audits, and scripts automating maintenance of multiple sites. We discuss the criteria for choosing a WordPress theme in a separate article.

List installed plugins:

wp plugin list

List plugins requiring updates:

wp plugin list --update=available

Update all plugins:

wp plugin update --all

Install and activate a plugin:

wp plugin install wordfence --activate

Deactivate a plugin – e.g., after an error:

wp plugin deactivate wordfence

Delete inactive plugins:

wp plugin delete $(wp plugin list --status=inactive --field=name)

List themes:

wp theme list

Activate a theme:

wp theme activate flavor

Update all themes:

wp theme update --all

4. Database

The database is the heart of WordPress. WP-CLI provides tools for export, import, optimization, cleanup, and search-replace operations – the latter cannot be safely performed with other tools because WP-CLI correctly handles PHP serialized data. More on MySQL administration and PostgreSQL on the service pages.

Export the database (SQL dump):

wp db export backup.sql

Import a database:

wp db import backup.sql

Optimize tables – equivalent to OPTIMIZE TABLE:

wp db optimize

Repair damaged tables:

wp db repair

Search-replace after migration – safely handles serialized data:

wp search-replace 'https://old-domain.com' 'https://new-domain.com' --all-tables --dry-run

Remove --dry-run after verification to perform the actual replacement.

Search-replace with per-table change report:

wp search-replace 'old-domain.com' 'new-domain.com' --all-tables --report-changed-only

Execute an SQL query directly:

wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type='revision'"

Delete post revisions – saves space:

wp post delete $(wp post list --post_type=revision --format=ids) --force

Delete expired transients:

wp transient delete --expired

Delete ALL transients:

wp transient delete --all

Count autoload records in wp_options:

wp db query "SELECT COUNT(*) FROM wp_options WHERE autoload='yes'"

Autoload size – a common cause of slow TTFB:

wp db query "SELECT SUM(LENGTH(option_value)) as autoload_size FROM wp_options WHERE autoload='yes'"

5. Users and permissions

Managing users with WP-CLI is faster than through the admin panel – especially for bulk operations, permission audits, and password resets after security incidents.

List users:

wp user list

Create a new user:

wp user create john john@domain.com --role=editor --display_name="John Smith"

Change a user's password:

wp user update admin --user_pass=newstrongpassword

Change a user's role:

wp user set-role john administrator

Delete a user and reassign their content to another:

wp user delete john --reassign=1

List administrators – security audit:

wp user list --role=administrator --fields=ID,user_login,user_email

Force log out all users – after a security incident:

wp user session destroy --all

6. Cron and scheduled tasks

WordPress has its own cron system (wp-cron) that runs scheduled tasks on page visits. On low-traffic sites, tasks can be delayed, and on high-traffic sites, wp-cron generates unnecessary load. WP-CLI lets you manage cron manually and move it to the system crontab.

List scheduled cron events:

wp cron event list

Run a specific event manually:

wp cron event run wp_update_plugins

Run all overdue events:

wp cron event run --due-now

Disable wp-cron and switch to system crontab:

wp config set DISABLE_WP_CRON true --raw

Then add to the server's crontab:

*/15 * * * * cd /var/www/html && wp cron event run --due-now --quiet

Delete an orphaned cron event – e.g., after removing a plugin:

wp cron event delete event_name

Check if wp-cron is disabled:

wp config get DISABLE_WP_CRON

7. Content and media

WP-CLI lets you manage posts, pages, comments, and the media library without opening the admin panel. Useful for bulk operations – deleting spam, regenerating thumbnails after a theme change, exporting content. Need a custom solution for content management? We are happy to help.

List posts:

wp post list --post_type=post --fields=ID,post_title,post_status

List pages with draft status:

wp post list --post_type=page --post_status=draft

Create a new post:

wp post create --post_type=post --post_title="New post" --post_status=publish

Delete spam comments:

wp comment delete $(wp comment list --status=spam --format=ids) --force

Count comments per status:

wp comment list --format=count --status=approved wp comment list --format=count --status=spam wp comment list --format=count --status=trash

Regenerate thumbnails – after a theme change or size adjustments:

wp media regenerate --yes

Regenerate thumbnails for a specific size only:

wp media regenerate --image_size=thumbnail --yes

Import images from a directory:

wp media import /path/to/images/*

8. Options and configuration

The wp_options table stores the configuration of WordPress, plugins, and the theme. WP-CLI lets you read, modify, and delete options without logging into the database.

Check site URL:

wp option get siteurl wp option get home

Change site URL – e.g., after migration:

wp option update siteurl 'https://new-domain.com' wp option update home 'https://new-domain.com'

Check the active theme:

wp option get template wp option get stylesheet

Disable comments globally:

wp option update default_comment_status closed

Check the value of any option:

wp option get blogdescription

List autoload options – diagnosing slow TTFB:

wp option list --autoload=on --fields=option_name,option_value --format=table | head -30

Delete orphaned options from a plugin:

wp option delete plugin_option_name

9. Cache and performance

WP-CLI gives you control over object cache, transients, and plugin caches directly from the terminal – without logging into the admin panel. More on caching strategies in the article Core Web Vitals Optimization and on the WordPress optimization service page.

Flush object cache:

wp cache flush

Delete expired transients:

wp transient delete --expired

Check object cache type – Redis, Memcached, file-based:

wp cache type

Flush WP Super Cache:

wp super-cache flush

Flush WP Rocket cache:

wp rocket clean --confirm

Run WP Rocket preload cache:

wp rocket preload

10. Automation and scripts

The true power of WP-CLI shows in bash scripts. You can automate complete site maintenance – updates, backup, cleanup, monitoring – and run it via cron or manually with a single command. Don't want to handle this yourself? As part of our WordPress care plans, we do it for you.

Script: backup + update + cleanup:

#!/bin/bash cd /var/www/html # Database backup wp db export /backup/db-$(date +%Y%m%d).sql # Update core, plugins, themes wp core update wp core update-db wp plugin update --all wp theme update --all # Cleanup wp transient delete --expired wp cache flush echo "Maintenance complete: $(date)"

Managing multiple sites in a loop:

for site in /var/www/site1 /var/www/site2 /var/www/site3; do echo "=== $site ===" wp plugin update --all --path="$site" wp core update --path="$site" done

Site status report – versions, plugins, updates:

echo "WordPress: $(wp core version)" echo "PHP: $(php -v | head -1)" echo "Plugins to update:" wp plugin list --update=available --fields=name,version,update_version

Daily cron with backup and updates:

0 3 * * * cd /var/www/html && bash /scripts/wp-maintenance.sh >> /var/log/wp-maintenance.log 2>&;1

Export plugin list to CSV – for documentation:

wp plugin list --format=csv > plugins-$(date +%Y%m%d).csv

Frequently Asked Questions

Yes – WP-CLI is an official tool, developed by the WordPress community and supported by Automattic. It is the standard in professional WordPress administration. The only risk comes from user errors – that is why for destructive operations (e.g., search-replace, deleting posts) you should use the --dry-run flag before actual execution.

It depends on the hosting provider. Most quality WordPress hosts have WP-CLI pre-installed. On cheap shared plans it may be unavailable – you need SSH access and PHP CLI. If your hosting does not provide WP-CLI, consider switching to a better provider.

WP-CLI search-replace correctly handles PHP serialized data – it updates both the text and the string length counters in serialization. Sed does not do this and will corrupt serialized data. For domain replacement in the WordPress database, always use WP-CLI, not sed.

Use the --path=/path/to/site flag with each command, e.g., wp plugin list --path=/var/www/site2. For WordPress Multisite, add --url=subdomain.domain.com to target a specific site in the network.

WP-CLI does not have a built-in undo. That is why before destructive operations you should always create a backup: wp db export backup.sql before search-replace, --dry-run before bulk changes. For updates – a staging environment lets you test changes before production.

Need help with WordPress management?

If you manage multiple WordPress sites and need support with maintenance automation, migrations, or server configuration – let's talk. No commitments, no marketing jargon – a concrete proposal after a short conversation.

Phone

+48 608 271 665

Mon–Fri, 8:00–16:00 CET

E-mail

kontakt@weboptimo.pl

We respond within 24h

Company

WebOptimo

VAT ID: PL6391758393