0
0
Wordpressframework~10 mins

Uninstall and cleanup hooks in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Uninstall and cleanup hooks
Plugin Installed
Plugin Activated
Plugin Used
User Chooses to Uninstall
Trigger uninstall.php
Run uninstall hook
Cleanup Database and Files
Plugin Fully Removed
This flow shows how WordPress runs uninstall hooks to clean up plugin data when a user uninstalls a plugin.
Execution Sample
Wordpress
<?php
// uninstall.php
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}
// Remove options
delete_option('my_plugin_option');
// Remove custom tables
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}my_plugin_table");
This code runs when the plugin is uninstalled to delete saved options and custom database tables.
Execution Table
StepActionCheckResultEffect
1Check if WP_UNINSTALL_PLUGIN is definedWP_UNINSTALL_PLUGIN defined?YesContinue uninstall
2Delete option 'my_plugin_option'Option exists?YesOption removed from database
3Drop custom table 'wp_my_plugin_table'Table exists?YesTable deleted from database
4Finish uninstall scriptNo more cleanupDonePlugin data fully cleaned
💡 Uninstall script ends after cleaning options and tables
Variable Tracker
VariableStartAfter Step 2After Step 3Final
WP_UNINSTALL_PLUGINdefineddefineddefineddefined
my_plugin_optionexistsdeleteddeleteddeleted
wp_my_plugin_tableexistsexistsdeleteddeleted
Key Moments - 2 Insights
Why do we check if WP_UNINSTALL_PLUGIN is defined?
This check ensures the uninstall script runs only when WordPress triggers uninstall, preventing direct access. See execution_table step 1.
What happens if the option or table does not exist?
The delete_option and DROP TABLE commands safely do nothing if the data is missing, so uninstall continues without errors. See execution_table steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe plugin option is deleted from the database
BThe uninstall script exits immediately
CThe custom table is dropped
DWP_UNINSTALL_PLUGIN is undefined
💡 Hint
Check the 'Action' and 'Effect' columns in execution_table row 2
At which step does the uninstall script confirm it is running inside WordPress uninstall?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'Check' column in execution_table row 1
If the custom table does not exist, what changes in the execution table?
AStep 2 result changes to 'No'
BStep 3 result changes to 'No', and table is not deleted
CStep 4 is skipped
DUninstall script exits at step 1
💡 Hint
Refer to execution_table step 3 'Check' and 'Result' columns
Concept Snapshot
WordPress uninstall hooks run when a plugin is removed.
Use uninstall.php with a check for WP_UNINSTALL_PLUGIN.
Delete options and custom tables to clean data.
This prevents leftover data after uninstall.
Always secure uninstall.php from direct access.
Full Transcript
When a WordPress plugin is uninstalled, WordPress runs the uninstall.php script if it exists. This script must check if the constant WP_UNINSTALL_PLUGIN is defined to confirm it is running during uninstall and not accessed directly. The script then deletes plugin options using delete_option and removes custom database tables with SQL DROP TABLE commands. This cleanup ensures no leftover data remains after the plugin is removed. The execution table shows step-by-step how the uninstall script checks conditions and deletes data safely. Variables like WP_UNINSTALL_PLUGIN, plugin options, and tables change state during the process. Key points include the importance of the WP_UNINSTALL_PLUGIN check and safe deletion commands that do nothing if data is missing. The visual quiz tests understanding of these steps and their effects.