Custom Scheduled Cleanup of Expired Transients
Last updated 1 month ago
PHP
function cleanup_expired_transients() {
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_%' AND option_value < UNIX_TIMESTAMP()");
}
// Schedule the event on plugin/theme activation.
if (!wp_next_scheduled('cleanup_expired_transients_hook')) {
wp_schedule_event(time(), 'daily', 'cleanup_expired_transients_hook');
}
add_action('cleanup_expired_transients_hook', 'cleanup_expired_transients');
// Clear the event on plugin/theme deactivation.
register_deactivation_hook(__FILE__, function () {
wp_clear_scheduled_hook('cleanup_expired_transients_hook');
});
All code snippets are licensed GPLv2 or later unless otherwise stated.