WordPress Snippets at WPcustoms

Delete multisites transients

Transients are a WordPress caching concept and temporarily save queries in a plain string. Many plugins and themes using transients and your database may end up with a lot of overhead infos. This function delete’s all blog’s db entries which contain _transient_ in their option name field. As always when dealing with the database – make sure to grab a backup of your sql db.

 * Snippet Name: Delete multisites transients
 * Snippet URL: https://wpcustoms.net/snippets/delete-multisites-transients/
  if ( is_multisite() ) {
    global $wpdb;
    $all_sites = $wpdb->get_results( "SELECT * FROM $wpdb->blogs" );
    if ( $all_sites ) {
        foreach ($all_sites as $site) {
            $wpdb->set_blog_id( $site->blog_id );
            $wpdb->query( "DELETE FROM '{$wpdb->prefix}options' WHERE 'option_name' LIKE ('_transient_%')" );