Using WordPress ‘delete_transient_{$transient}’ PHP action

The delete_transient_{$transient} is a WordPress Action that fires immediately before a specific transient is deleted.

Usage

add_action( 'delete_transient_$transient', 'your_function_name' );

function your_function_name( $transient ) {
	// your custom code here
	return $transient;
}

Parameters

  • $transient (string): The name of the transient that is being deleted.

More Information

See WordPress Developer Resources: delete_transient_{$transient}

Transients are a type of caching mechanism in WordPress that temporarily stores data in the database. This action can be used to perform any custom code or additional tasks before the specified transient is deleted.

Examples

Example 1

This example demonstrates how to use the delete_transient_{$transient} action to perform a custom task before deleting a specific transient.

add_action( 'delete_transient_hello', 'my_function' );

function my_function( $transient ) {
	// Do something before the transient is deleted
	// ...
	// Your custom code here
}

Example 2

This example demonstrates how to delete multiple transients using the delete_transient_{$transient} action.

add_action( 'delete_transient_hello', 'my_function' );
add_action( 'delete_transient_world', 'my_function' );

function my_function( $transient ) {
	// Your custom code here to perform additional tasks before deleting the transients
	// ...
}

Example 3

This example demonstrates how to use the delete_transient_{$transient} action to modify the transient data before it is deleted.

add_action( 'delete_transient_hello', 'my_function' );

function my_function( $transient ) {
	// Get the transient data before it is deleted
	$transient_data = get_transient( $transient );

	// Modify the transient data as needed
	$transient_data['modified_data'] = 'Some new modified data';

	// Update the transient with modified data
	set_transient( $transient, $transient_data, 60 * 60 );

	// Your custom code here to perform additional tasks before deleting the transient
	// ...
}

Example 4

This example demonstrates how to use the delete_transient_{$transient} action to send an email notification before deleting a specific transient.

add_action( 'delete_transient_hello', 'my_function' );

function my_function( $transient ) {
	// Get the user email address from the transient data
	$user_email = get_transient( $transient );

	// Send email notification to the user
	wp_mail( $user_email, 'Your transient data is about to be deleted', 'Your transient data is about to be deleted. Please take necessary action.' );

	// Your custom code here to perform additional tasks before deleting the transient
	// ...
}

Example 5

This example demonstrates how to use the delete_transient_{$transient} action to log the deletion of a specific transient.

add_action( 'delete_transient_hello', 'my_function' );

function my_function( $transient ) {
	// Log the deletion of the transient
	error_log( 'Transient ' . $transient . ' deleted on ' . date( 'Y-m-d H:i:s' ) );

	// Your custom code here to perform additional tasks before deleting the transient
	// ...
}