Concept of Plugin Deactivation Hook in WordPress Tutorial

Reading Time: 6 minutes

Plugins are essential in the world of WordPress development for increasing the functionality of your website. While plugin activation allows you to add new functionality, plugin deactivation allows you to completely uninstall or deactivate a plugin.

The “deactivation hook” is the mechanism that permits this procedure, and we will look at how it works in WordPress in this lesson.

By the end of this article, you’ll have a solid understanding on how the plugin deactivation hook works, as well as the ability to employ it in your WordPress projects to ensure a clean and efficient deactivation procedure.

Read More: Complete Concept of Plugin Activation Hook in WordPress

Let’s get started.

What is WordPress Plugin Deactivation Hook?

WordPress plugins can add or alter functionality within the WordPress system by utilising hooks and filters. In the context of WordPress, a plugin deactivation hook is an action hook that is fired when a plugin is deactivated or turned off.

When a WordPress plugin is deactivated, the deactivate_plugin function is invoked, which causes the deactivate_plugin hook to be triggered. When a plugin is deactivated, plugin writers can connect their own custom functions or actions to this hook to do certain activities.

When a user deactivates a plugin, this might be useful for things such as cleaning out plugin-specific data or settings.


Here’s an example of how you can use the register_deactivation_hook() function in WordPress:

function my_plugin_deactivation_action() {

    // Perform actions when the plugin is deactivated
    // This could include removing options, cleaning up data, etc.

register_deactivation_hook(__FILE__, "my_plugin_deactivation_action");

It is crucial to note that not all plugins have deactivation hooks, and the availability and use of such hooks is dependent on the development of the given plugin. Developers can utilise these hooks to make their plugins more user-friendly and to ensure that they clean up correctly when they are deactivated.

Plugin Deactivation: How To Drop a Database Table?

To drop a database table when a WordPress plugin is deactivated, you have to do

function my_plugin_deactivation() {

    global $wpdb;

    $table_name = $wpdb->prefix . 'your_table_name'; // Replace 'your_table_name' with your actual table name

    $sql = "DROP TABLE IF EXISTS $table_name";

register_deactivation_hook(__FILE__, 'my_plugin_deactivation');

In the above code, we’re using $wpdb->prefix to ensure that the table name is prefixed with the WordPress database prefix. Replace your_table_name with the actual name of the database table that your plugin creates.

Please exercise caution when using this code, as dropping a database table will permanently delete its data, and there’s no way to recover it once the table is dropped.

Read More: How To Stop Spam User Registration in WordPress Using Plugin

Plugin Deactivation: How To Remove a WordPress Page?

To remove a WordPress page when a plugin is deactivated, you have to do

function delete_page_on_deactivation() {
    $page_id_to_delete = 123; // Replace with the actual post ID of the page you want to delete

    // Check if the post exists and is a page
    if (get_post_type($page_id_to_delete) === 'page') {
        // Delete the page
        wp_delete_post($page_id_to_delete, true);

register_deactivation_hook(__FILE__, 'delete_page_on_deactivation');

Replace 123 with the actual post ID of the page you want to delete.

Please be cautious when using this code, as it will permanently delete the specified page, and there’s no way to recover it once it’s deleted.

Plugin Deactivation: How To Remove a Custom Role in WordPress?

To remove custom roles in WordPress when a plugin is deactivated, you have to do

function remove_custom_roles_on_deactivation() {

    remove_role('custom_role_1'); // Replace with the name of your custom role
    remove_role('custom_role_2'); // Replace with the name of another custom role, if applicable
    // Add more remove_role lines as needed for each custom role you want to remove

register_deactivation_hook(__FILE__, 'remove_custom_roles_on_deactivation');

Replace ‘custom_role_1‘ and ‘custom_role_2‘ with the actual role names you want to remove.

Please be cautious when using this code, as it will permanently remove the custom roles, and there’s no way to recover them once they’re removed.

That’s it.

We hope this article helped you to learn about Concept of Deactivation Hook in WordPress Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more