Using WordPress ‘delete_site_transient()’ PHP function

The delete_site_transient() WordPress PHP function deletes a site transient, which is temporary data stored in the database, scoped to the entire network on a multisite installation. This function is used to remove a specific transient by name.

Usage

Using delete_site_transient() function is quite straightforward. You just need to pass the name of the transient you want to delete.

delete_site_transient( 'my_site_transient' );

In the example above, ‘my_site_transient’ is the name of the site transient that will be deleted.

Parameters

  • $transient (string) (required): The name of the site transient to be deleted. It is expected to not be SQL-escaped.

More information

See WordPress Developer Resources: delete_site_transient()

This function was implemented in WordPress 3.0. It has not been deprecated and is still actively used. The source code for this function can be found in wp-includes/option.php.

Examples

Deleting a Basic Site Transient

This example shows how to delete a simple site transient named ‘my_site_transient’.

// Deleting 'my_site_transient'
delete_site_transient( 'my_site_transient' );

After running this code, the ‘my_site_transient’ will be removed from the database.

Deleting a Site Transient After Checking Its Existence

This example demonstrates how to check for the existence of a site transient and then delete it.

// Check if 'my_site_transient' exists
if ( get_site_transient( 'my_site_transient' ) ) {
    // If it exists, delete it
    delete_site_transient( 'my_site_transient' );
}

This ensures that the transient ‘my_site_transient’ is deleted only if it exists.

Deleting Multiple Site Transients

This example demonstrates how to delete multiple site transients.

// Array of transients to delete
$transients_to_delete = array( 'transient_1', 'transient_2', 'transient_3' );

// Loop through the array and delete each transient
foreach ( $transients_to_delete as $transient ) {
    delete_site_transient( $transient );
}

This will delete ‘transient_1’, ‘transient_2’, and ‘transient_3’ from the site transients.

Deleting a Site Transient within a Function

This example shows how to encapsulate the deletion of a site transient within a custom function.

function remove_my_transient() {
    // Delete 'my_site_transient'
    delete_site_transient( 'my_site_transient' );
}

You can now call remove_my_transient() whenever you want to delete ‘my_site_transient’.

Deleting a Site Transient on a Specific Hook

This example demonstrates how to delete a site transient when a specific action hook is triggered.

// When 'save_post' action is triggered, delete 'my_site_transient'
add_action( 'save_post', function() {
    delete_site_transient( 'my_site_transient' );
});

In this case, ‘my_site_transient’ will be deleted every time a post is saved.