When working with custom tables in WordPress, we need to have a way to clean the database if the users decide to delete our plugin or we need it ourselves. In this tutorial, we’ll use the same installer class from previous tutorials and update it with new methods.

If we are working in a plugin with custom tables, WordPress provides an easy way to call our delete methods. This is done by adding an uninstall.php file inside of your main plugin folder. This file will be called right before the plugin is removed from our site.

There is also a deactivation hook available, but I would not recommend clearing the database from your custom tables when a plugin is deactivated. This could mean the user is debugging something.

Inside of the uninstall.php file, we can have something like this:


if( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) exit();

require_once trailingslashit( plugin_dir_path( __FILE__ ) ) . 'path_to_installer_class.php';

// Namespaced class from previous tutorials.


To uninstall our plugin correctly, we need to remove any custom tables in WordPress, delete all options added by our own solution and also delete any other data in other WordPress tables.

So, here is our uninstall method.


In this tutorial, we’ve learned how to delete our custom tables and also any other hanging data that would not keep the database clean.

So far, we have learned how to install custom tables, work with custom meta data and how to delete them. The next tutorial regarding custom tables in WordPress will be using everything we’ve learned and create our own custom solution for it, together with other classes to access such.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.