Does WordPress delete expired transients from the database?

Just the other day in Caching using the WordPress Transient API, I explained about using the WordPress Transient API to cache data in the database temporarily, the idea being that you set an expiry date and time against each, and WordPress automatically expires them when this is reached.

So far so good, right? Not entirely. When I was checking to make sure that the expired transients were being cleared properly from the  Smart Insights database, I discovered that the wp_options table was now 130,000 records larger than it was when I launched the transient functionality back on Monday. A little more digging showed that every transient record created since Monday was still in the DB, including all those with an expiry date in the past.

WordPress does not seem to be deleting expired transients from the database.

So why are transients not being deleted automatically?

A little googling brought me to a post from WPEngine on the subject;

But with the WordPress Transients, you get different but still very undesirable behavior. Because the values are written to the database, not a fixed-sized block of RAM, they all stick around. Which means even with the heavily-loaded site, you still have your session data. Awesome!

Or so you thought. Because what isn’t said in the Transient API is what happens when you use unique keys like sessions. And what happens in with the built-in method is that the options table fills up indefinitely! Because: WordPress’s “old data clean up” only operates when you request the key (as we covered earlier). If you just leave the key, it’s left in the options table, forever. There’s no separate process that cleans these up!

More at A Technical Transients Treatise at WPEngine

In my case, since I’m appending the visitor’s PHP session ID to set or check for transients, that rarely happens. So my database gets filled up with lots of expired and redundant data.

What’s the solution to removing transients automatically?

Put simply, you have to remove them yourself – but never fear – the wonderful plugin community comes to the rescue in the form of a plugin to handle this for you. Grab the plugin Purge Transients from Github, put it in your plugins folder and activate it in your WordPress admin. Now any expired transients will be deleted from your database after 7 days.

If you want to force WordPress delete expired transients sooner (like me), open up the purge-transients.php file in a text editor and change the following line to your desired timeframe;

function purge_transients($older_than = '7 days', $safemode = true) {

Hooray! No more unnecessary redundant data clogging up the wp_options table.

Published by

Stu Miller

Web consultant and specialist, WordPress developer and PHP developer based in Leeds, UK. 15 years experience in architecting web sites and applications. Co-founder and Technical Director of SmartInsights.com, formerly the same of First 10 Digital

2 thoughts on “Does WordPress delete expired transients from the database?”

Leave a Reply