Using WordPress ‘delete_transient()’ PHP function

The delete_transient() WordPress PHP function deletes a transient from the options database table.

Usage

Here’s how to use delete_transient():

delete_transient('my_transient');

In this case, ‘my_transient’ is the name of the transient we want to delete.

Parameters

  • $transient (string, required): The name of the transient we want to delete. This value is expected to not be SQL-escaped.

More information

See WordPress Developer Resources: delete_transient()

This function is used for invalidating cached data, such as posts, terms, users, comments, etc., which is retrieved from transients. You can trigger this function by using various action hooks, depending on when you want the cache to be invalidated.

Examples

Deleting a Transient after Saving a Post

add_action('save_post', 'wpdocs_delete_my_important_transient');

function wpdocs_delete_my_important_transient() {
    delete_transient('wpdocs_my_transient_name');
}

In this example, every time a post is saved, the transient ‘wpdocs_my_transient_name’ is deleted.

Deleting a Transient after Deleting a Post

add_action('deleted_post', 'wpdocs_delete_my_important_transient');

function wpdocs_delete_my_important_transient() {
    delete_transient('wpdocs_my_transient_name');
}

In this example, every time a post is deleted, the transient ‘wpdocs_my_transient_name’ is deleted.

Deleting a Transient after Editing a Post

add_action('edit_post', 'wpdocs_delete_my_important_transient');

function wpdocs_delete_my_important_transient() {
    delete_transient('wpdocs_my_transient_name');
}

In this example, every time a post is edited, the transient ‘wpdocs_my_transient_name’ is deleted.

Deleting a Transient for Custom Post Types

add_action('save_post_book', 'wpdocs_delete_my_important_transient');

function wpdocs_delete_my_important_transient() {
    delete_transient('wpdocs_my_transient_name');
}

In this example, every time a ‘book’ post type is saved, the transient ‘wpdocs_my_transient_name’ is deleted.

Deleting a Transient when a Term is Edited

add_action('edit_term', 'wpdocs_edit_term_delete_transient');

function wpdocs_edit_term_delete_transient() {
    delete_transient('special_query_results');
}

In this example, whenever a term (category, tag, etc.) is edited, the transient ‘special_query_results’ is deleted.