Delete Transients Using Wp Cli

Page content

How to delete transients in Wordpress using WP-CLI

In Wordpress, transients are used to cache data for a specific period of time. The transients have a fixed maximum lifetime after which they expire and get deleted. Transient data in Wordpress is stored only temporarily. However, sometimes you may need to manage transients manually or using a plugin because issues may crop up with transients.

For example, despite having cleared your cache, you may see that the popular posts or share counts are not showing up. Clearing expired transients helps in such a condition. Clearing out the expired transients can also help optimize Wordpress database and manage overall website performance.

You can delete expired transients using a database management and cleanup plugin. However, if you do not want to use a plugin, there is an easier method to clear transients. You can do it using the WP-CLI. You will need ssh access to your server and WP-CLI installed on your server to clear transients.

Install WP CLI

Installing WP CLI is easy. You can install it locally if you are developing locally or on your server. You will only need ssh connection to your server and with a few commands, the CLI can be installed.

First, you will need to download wp-cli.phar using wget or curl:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Now, make the file executable and move it somwhere in your PATH:

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Now, run wp –info and you will see an output like the following showing the cli has been successfully installed:

OS:     Linux 6.5.0-1018-aws #18~22.04.1-Ubuntu SMP Fri Apr  5 17:44:33 UTC 2024 x86_64
Shell:  /bin/bash
PHP binary:     /usr/bin/php8.1
PHP version:    8.1.2-1ubuntu2.17
php.ini used:   /etc/php/8.1/cli/php.ini
MySQL binary:   /usr/bin/mysql
MySQL version:  mysql  Ver 15.1 Distrib 10.6.16-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /home/ubuntu
WP-CLI packages dir:
WP-CLI cache dir:       /home/ubuntu/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.10.0

Clear Expired Transients

Now, change directory to the root directory of your wordpress website. You need to make sure that it is a wordpress installation and you are running commands from the root directory of your wordpress website.

cd /var/www/html

Now, you can run wp-cli commands. To clear expired transients, run:

wp transient delete --expired

You will receive an output like the following showing that expired transients have been successfully deleted.

Success: 12 expired transients deleted from the database.

Clear All Transients

To clear all the transients, simply run the following command:

wp transient delete --all
Success: 14 transients deleted from the database.