If the uninstall.php file is missing and no uninstall hook is registered, WordPress does not run any cleanup code automatically. Plugin data remains in the database until manually removed.
The correct function to register an uninstall hook is register_uninstall_hook with the plugin file path and callback function.
if (!defined('WP_UNINSTALL_PLUGIN')) {
exit();
}
delete_option('my_plugin_option');Why might the uninstall hook not run when uninstalling the plugin?
if (!defined('WP_UNINSTALL_PLUGIN')) { exit(); } delete_option('my_plugin_option');
WordPress only runs uninstall.php if it is located in the root folder of the plugin. If placed elsewhere, it won't run and no cleanup happens.
function cleanup() {
delete_option('plugin_setting');
delete_option('plugin_cache');
}
register_uninstall_hook(__FILE__, 'cleanup');After uninstalling the plugin, what happens to these options?
function cleanup() {
delete_option('plugin_setting');
delete_option('plugin_cache');
}
register_uninstall_hook(__FILE__, 'cleanup');The uninstall hook runs the cleanup function which calls delete_option on both options, removing them from the database.
uninstall.php is executed when the plugin is deleted directly, even if not deactivated first, ensuring cleanup always runs.
