Skip to content

Lesson 3 of 9 · 22m

Activation, Deactivation, and Uninstall: Owning the Lifecycle

What belongs in activation hooks, how to version schema migrations, when not to delete data, and why uninstall.php is a contract with site owners.

The fastest way to earn one-star reviews is to mishandle lifecycle events. The two classic failures are opposites: a plugin that destroys years of business data when someone clicks Deactivate, and a plugin that litters the database forever because uninstalling it was too dangerous to automate. Professional plugins are explicit about which state is temporary, which is durable, and which must be reversible.

Activation is for setup, not traffic work

register_activation_hook(__FILE__, 'acme_activate');

function acme_activate(bool $network_wide): void {
    if (!current_user_can('activate_plugins')) {
        return;
    }
    acme_maybe_install_schema();
    update_option('acme_version', ACME_VERSION, false);
    flush_rewrite_rules();
}

Activation callbacks run in a fragile context. Keep them fast and deterministic. Do not send email, crawl the site, call remote APIs, or create thousands of rows. Heavy initialization belongs in a queued background job after the plugin is active.

If the plugin registers post types or rewrite rules, the order matters: register the structures, then flush rewrites. Flushing alone does nothing if your post type is not registered in that activation request.

Deactivation is a pause button

Deactivation should disable behavior — remove scheduled hooks, stop runtime integrations, flush caches or rewrites — but preserve user data. If deactivation deletes orders, licenses, forms, or logs, operators will test upgrades in fear and eventually replace you.

register_deactivation_hook(__FILE__, 'acme_deactivate');

function acme_deactivate(): void {
    wp_clear_scheduled_hook('acme_sync_inventory');
    flush_rewrite_rules();
}

Uninstall means the site owner chose removal

uninstall.php is not a cleanup suggestion. It executes after someone deliberately uninstalls the plugin. The decision model should be conservative: delete plugin-owned options, transient records, custom tables, cron events, and plugin capabilities. Ask hard questions before deleting content that users may consider theirs.

<?php
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}

delete_option('acme_settings');
delete_site_option('acme_network_settings');

global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}acme_inventory_log");

wp_clear_scheduled_hook('acme_sync_inventory');

If your data is valuable or shared, give users an explicit setting such as “Remove all Acme data on uninstall.” Otherwise preserve business records and document what remains. On multisite, uninstalling a network-activated plugin is more complicated; iterate sites carefully or provide a WP-CLI cleanup command for large networks.

Schema migrations are versioned, idempotent, and resumable

Never infer migration state from whether a table exists. Store a schema version, compare it, and run ordered migrations:

$installed = get_option('acme_schema_version', '0');
if (version_compare($installed, ACME_SCHEMA_VERSION, '<')) {
    acme_maybe_install_schema();
    update_option('acme_schema_version', ACME_SCHEMA_VERSION, false);
}

Each migration must tolerate being run twice because requests fail, operators restore backups, and multisite upgrades partially complete. Use dbDelta() for core-style table creation; use explicit ALTER TABLE only when you have verified existing indexes and engine behavior.

Field notes

  • Store options with autoload disabled for large values; autoloaded options are read on every request.
  • Flush rewrite rules sparingly; doing it on every init is a performance bug.
  • Test activation with PHP errors displayed, object cache enabled, and wp-cron disabled — that is closer to production.
  • Never assume register_uninstall_hook() runs on multisite the way uninstall.php documentation suggests; test the exact network activation state.

Was this lesson helpful?

Sign in to rate