Performance: Uninstall and cleanup hooks
This affects page load speed and backend performance by ensuring no leftover data or processes slow down the site after plugin removal.
<?php register_uninstall_hook(__FILE__, 'my_plugin_uninstall'); function my_plugin_uninstall() { delete_option('my_plugin_option'); global $wpdb; $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_plugin_table"); } ?>
<?php // No uninstall hook, data remains in database // Plugin deactivated but options and custom tables remain ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No uninstall cleanup | No change | 0 | 0 | [X] Bad |
| Proper uninstall hook cleanup | No change | 0 | 0 | [OK] Good |
register_uninstall_hook in a WordPress plugin?register_uninstall_hookregister_uninstall_hook with the plugin file and cleanup function.function cleanup_plugin() {
delete_option('plugin_setting');
delete_transient('plugin_cache');
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}plugin_data");
}
What will happen when the plugin is uninstalled?register_uninstall_hook('my-plugin/my-plugin.php', 'cleanup_plugin');
function cleanup_plugin() {
delete_option('my_plugin_option');
}wp_custom_data. Which of the following uninstall functions correctly performs this cleanup?Pass the test and this pattern moves unknown