WordPress plugins and themes can store a lot of temporary data known as transients. By default these transients are stored in the wp_options table and can accumulate rather quickly. Recently on codeable I cleaned a site with 58000 expired transients that were causing the database to crash daily. Due to an idiosyncrasy in WordPress core, expired transients are only removed from the database if access to them is attempted, if no access to them is made then the transients stay in the wp_options table. This tutorial will show you how to delete expired transients using WP-CLI from wp_options or your external object cache and automate the deletion of these transients ever day using a cronjob.
Automatically Delete Expired WordPress Transients using WP-CLI
Deleting expired transients from your WordPress database with WP-CLI is very easy
wp transient delete --expired
If you use an external object cache like Redis Memcached or PHP-APCu, then you empty it with this command
wp cache flush
So you do not have to do this manually, I put this in a real cronjob
For users without an external object cache use this cronjob, replace
www-data with your user and
/var/www/guides.wp-bullet.com with your WordPress path
@daily sudo -u www-data wp transient delete --expired --path=/var/www/guides.wp-bullet.com
If you are using an object cache then you can use this cronjob to flush the object cache using WP-CLI
@daily sudo -u www-data wp cache flush --path=/var/www/guides.wp-bullet.com
Now you won’t have to worry about expired transients accumulating and potentially slowing down your site :).