delete_transient( string $transient ): bool

Deletes a transient.

Parameters

$transientstringrequired
Transient name. Expected to not be SQL-escaped.

Return

bool True if the transient was deleted, false otherwise.

Source

function delete_transient( $transient ) {

	/**
	 * Fires immediately before a specific transient is deleted.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 3.0.0
	 *
	 * @param string $transient Transient name.
	 */
	do_action( "delete_transient_{$transient}", $transient );

	if ( wp_using_ext_object_cache() || wp_installing() ) {
		$result = wp_cache_delete( $transient, 'transient' );
	} else {
		$option_timeout = '_transient_timeout_' . $transient;
		$option         = '_transient_' . $transient;
		$result         = delete_option( $option );

		if ( $result ) {
			delete_option( $option_timeout );
		}
	}

	if ( $result ) {

		/**
		 * Fires after a transient is deleted.
		 *
		 * @since 3.0.0
		 *
		 * @param string $transient Deleted transient name.
		 */
		do_action( 'deleted_transient', $transient );
	}

	return $result;
}

Hooks

do_action( ‘deleted_transient’, string $transient )

Fires after a transient is deleted.

do_action( “delete_transient_{$transient}”, string $transient )

Fires immediately before a specific transient is deleted.

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Cache invalidation

    If data (posts, terms, users, comments, etc.) has been updated, previously cached data, being retrieved from transients, might be invalid.

    Flush explicitly corresponding transients using the following action hooks:

    • in case a new post has been created, use save_post or publish_post:
      add_action( 'save_post', 'wpdocs_delete_my_important_transient' );
      function wpdocs_delete_my_important_transient() {
      	// check status here or check if post data has been changed
      	delete_transient( 'wpdocs_my_transient_name' );
      }
    • in case any post has been deleted, use deleted_post:
    • add_action( 'deleted_post', 'wpdocs_delete_my_important_transient' );
      function wpdocs_delete_my_important_transient() {
      	delete_transient( 'wpdocs_my_transient_name' );
      }
    • when post has been edited, edit_post
      add_action( 'edit_post', 'wpdocs_delete_my_important_transient' );
      function wpdocs_delete_my_important_transient() {
      	delete_transient( 'wpdocs_my_transient_name' );
      }
    • in case of custom post types, e.g. book, you can use edit_post_book, save_post_book

    NOTE: In some cases you might need to check post status, for example, when you move post to trash or restore it.

  2. Skip to note 4 content

    Clearing our transient via the edit_term hook

    // Create a simple function to delete our transient
    function wpdocs_edit_term_delete_transient() {
         delete_transient( 'special_query_results' );
    }
    // Add the function to the edit_term hook so it runs when categories/tags are edited
    add_action( 'edit_term', 'wpdocs_edit_term_delete_transient' );

You must log in before being able to contribute a note or feedback.