WordPress Snippet – Clear Transients
Transients are a wonderful way to temporarily cache pieces of information within WordPress. They are very flexible so you can store objects, strings, JSON.. anything really! They are used commonly in widget plugins whereby content is generated or, more commonly, pulled from a remote API. The reason being because API calls take time and often the data doesn’t change for a while at a time (Tweets for instance.. unless you’re Stephen Fry in which case updating every page refresh might be a good idea!).
Sadly, sometimes this information gets stuck and an empty string or broken object might be stored in a transient. Now WP doesn’t care what’s stored in a transient so it will happily store nothing or something broken and set it to expire at a predefined time as normal. This isn’t wonderful if you are storing a fatal error or something which causes the site to display an error or incorrect information.
The following snippet of PHP is a simple query to delete all transients from the database meaning the next page refresh will pull the new information from the APIs and regenerate the transient thus fixing whatever error was showing on the site. This is especially useful when clients are roaring at you on the phone to fix something.. it could be a one hit fixes all approach.. or it might not do anything. All I can guarantee is that it won’t break thing on your site as we are deleting a cache in effect… all original data and mechanisms to restore the transient remain in tact.
Simply add this to your functions.php file in your theme, visit the site and then delete the code again.. couldn’t be easier really. I believe people have made plugins out of this but I don’t really think a plugin is necessary for the sake of a once in a blue moon activity.
global $wpdb; $sql = 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE "_transient_%"'; $wpdb->query($sql);