Clear WordPress Custom Transient Values

I’ve seen an article saying that in order to delete WordPress feed caches, the following code would work.

global $wpdb;
$wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%_feed_%')" );

So how do I apply this to my own transient caches?

Read More

If I save data like,

set_transient('mytransient_oneminutecache', $data1, 60);
set_transient('mytransient_onehourcache', $data2, 60 * 60);
set_transient('mytransient_12hourcache', $data3, 60 * 60 * 12);

Then does this work?

global $wpdb;
$wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%mytransient_%')" );

Or should it be

global $wpdb;
$wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%_mytransient_%')" );

?

I’m not used to view data base tables so if you can tell if it works or not or how to view inside the option table, it would be appreciated.

Related posts

Leave a Reply

2 comments

  1. Okay, I think I got it.

    First I ran this

    $data1 ='hi';
    $data2 ='hello';
    $data3 ='bye';
    
    set_transient('mytransient_oneminutecache', $data1, 60);
    set_transient('mytransient_onehourcache', $data2, 60 * 60);
    set_transient('mytransient_12hourcache', $data3, 60 * 60 * 12);
    

    Next opened, http://[site address]/wp-admin/options.php

    then found these are saved:

    _transient_mytransient_12hourcache
    _transient_mytransient_onehourcache
    _transient_mytransient_oneminutecache

    _transient_timeout_mytransient_12hourcache
    _transient_timeout_mytransient_onehourcache
    _transient_timeout_mytransient_oneminutecache

    So after that, I ran this and they were gone!

    global $wpdb;
    $wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%_mytransient_%')" );
    $wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient_timeout%_mytransient_%')" );
    
  2. $wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%\_mytransient\_%')" );
    

    is probably better because an _ is a single-character wildcard so needs escaping .