Created
February 16, 2024 07:42
-
-
Save spenserhale/0e794120d246c51ea5386c63147b2fde to your computer and use it in GitHub Desktop.
delete_transients() - WP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Deletes all current site transients. (Blog not Network) | |
* | |
* @return array{ deleted: string[], errored: string[] } An array of successfully and failed to delete transients. | |
*/ | |
function delete_transients(): array | |
{ | |
if (wp_using_ext_object_cache()) { | |
global $wp_object_cache; | |
$prefix = sprintf(':%d:transient:', get_current_site_id()); | |
$transients = []; | |
foreach ($wp_object_cache->mc['default']->getAllKeys() ?? [] as $transient) { | |
if (str_contains($transient, $prefix)) { | |
/** @noinspection OffsetOperationsInspection */ | |
$transients[] = explode($prefix, $transient)[1]; | |
} | |
} | |
} else { | |
global $wpdb; | |
$transients = $wpdb->get_col("SELECT option_name FROM $wpdb->options WHERE option_name LIKE '_transient_%'"); | |
$transients = array_map(static fn($key) => str_replace('_transient_', '', $key), $transients); | |
} | |
$deleted = []; | |
$errored = []; | |
foreach ($transients as $transient) { | |
if (delete_transient($transient)) { | |
$deleted[] = $transient; | |
} else { | |
$errored[] = $transient; | |
} | |
} | |
return ['deleted' => $deleted, 'errored' => $errored]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment