Delete all transients from your WordPress multisite

Someone asked how he could delete all the transients from a WordPress multisite, as I wrote the code for him, thought to share it here for everyone :)

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_%')" );
        }
    }
}

2 thoughts on “Delete all transients from your WordPress multisite

  1. Pingback: Delete all transients from your WordPress multisite – SecondHack

  2. Greetings, Thank you for the code. It works perfect. I added a couple more conditions to get rid of the new “nextgen gallery” crap.

    Cheerz

    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_%')
    or `option_name` LIKE ('_site\_transient\_%')
    or `option_name` LIKE ('displayed\_galleries\_%')
    " );
    }
    }
    }

    ?>

Leave a Reply