Transients are extremely useful – most WordPress developers use them to cache frequently used data, but many don’t realize that transients aren’t automatically deleted once they expire and can slow down the site.

If you are not 100% familiar with the WordPress Transient API then feel free to revisit the well documented WordPress codex entry.

The Problem

The main problem about WordPress transients is that once they expire they are not automatically deleted. They are only deleted when you try to retrieve a transient that has an expired value. So technically, if you create a transient and then never reference again, it will remain in your database forever. This can drastically increase the size of your wp_options table, which in turn can slow down your site. So how do we fix this problem and delete expired transients from the wp_options table?

The Solution

To purge expired transients on a daily basis, simply add the code below into the functions.php file or any custom functions file called by the theme. Or, create a small plugin file to handle it on its own.

The above code will delete all expired transients on a daily basis, but you can adjust it to a schedule that fits your needs. This is extremely useful for developers who use dynamic transients (i.e. storing data based on someone’s IP address as a transient value).

Please let us know your thoughts and if you know a better way to solve this problem :)

4 Responses to "How To Purge All Expired Transients in WordPress"

Leave a Reply

Your email address will not be published.