WordPress Snippet – Clear Transients

April 14, 2014 | Bug Fixes, PHP, Time Savers, Troubleshooting, Wordpress | 0 comments

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.

The Code

global $wpdb;
$sql = 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE "_transient_%"';

A Donate Button!


Submit a Comment

Your email address will not be published. Required fields are marked *

CommentLuv badge

Divi United

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!