What is the uninstall.php file in WordPress

uninstall.php file in WordPress

A WordPress plugin that adds its data to the database should clean it up after the user deletes it. To do this, the engine has a simple and convenient tool – the uninstall.php file. Let’s analyze what it can do and how it can be used by a WordPress developer.

How uninstall.php works in WordPress

It is placed in the root directory of the plugin. For example, the add-on is in the folder


With a deleted file, it will have the structure:


When the user clicks Uninstall in the admin panel, the engine looks for the uninstall.php file and executes it. This allows plugin developers to clean up “garbage” from the database.

What can uninstall.php do

It can contain any code needed to clear and remove all plugin data. But first of all, it must have protection from unwanted access.

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

This line should be at the very beginning of it. In toga, if anything other than WordPress tries to access the file, the script will exit immediately.

An example of uninstall.php that removes several types of data:

if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) exit;
// remove plugin options
// delete intermediate data
// delete cron events
// remove anything else
It will be correct to clear all the data from their database, as well as the file structure of the module.

Removing Plugin Options

Often plugins create their own options. To remove them, use the function delete_option(). For example, to remove an option with the name add_comments_options, you can use this code:

delete_option( 'add_comments_options');

To remove multiple options, you can use an array:

$options = array(
foreach ( $options as $option ) {
if ( get_option( $option ) ) delete_option( $option );
Replace add_comments_options_1the others with your own names.

Removing intermediate data (transients)

They are also stored in the WordPress database. There is a function for this delete_transient():

delete_transient( 'add_comments_transient' );

For mass cleaning:

$transients = array(
foreach ( $transients as $transient ) {
delete_transient( $transient );

Deleting cron events

The function is used wp_unschedule_event(). For example:

$timestamp = wp_next_scheduled( 'add_comments_cron_event' );
wp_unschedule_event( $timestamp, 'add_comments_cron_event' );

Deleting database tables

To delete a table named add_comments_table:

global $wpdb;
$table_name = $wpdb->prefix .'add_comments_table';
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );

wpdb is a WordPress class for database table operations.

Deleting posts and pages

The function is used for this wp_trash_post(). For example, to delete a post with ID=123:

wp_trash_post( 123 );

You need to delete posts and pages if you are absolutely sure that the user no longer needs them. One way to find out is to add an option on the plugin’s Settings page asking “Delete posts/posts when uninstalling the plugin?”

Deleting custom post types

Often in the plugin, you need to create your own post type. When removing the add-on, these entries must also be deleted. For this use wp_delete_post(). For example, to remove all entries that are of a custom type add_comments_cpt:

$add_comments_cpt_args = array( 'post_type' => 'add_comments_cpt', 'posts_per_page' => -1 );
$add_comments_cpt_posts = get_posts( $add_comments_cpt_args );
foreach ( $add_comments_cpt_posts as $post ) {
wp_delete_post( $post->ID, false );

The function get_posts()selects all records that are of our type. Then they are iterated over and removed by the function wp_delete_post(). add_comments_cptSubstitute your own value in the code.

Also see: How to Password Protect WordPress Uploads

Comments, meta fields, taxonomies of these posts will also be removed.

The parameter false tells the engine to put the posts in the trash. If you want to delete them permanently, put true.

Removing user metadata

There is for this delete_user_meta(). For example, you need to remove all metadata for a user named add_comments_user_meta:

$users = get_users();
foreach ( $users as $user ) {
delete_user_meta( $user->ID, 'add_comments_user_meta' );
get_users()- Gets an array of all registered users. Then it goes through and the function delete_user_meta()removes all user metadata with the name add_comments_user_meta.

Removing post metadata

Post metadata is removed by the delete_post_meta(). Let’s delete, for example, all meta with the name add_comments_post_meta:

$add_comments_post_args = array( 'posts_per_page' => -1 );
$add_comments_posts = get_posts( $add_comments_post_args );
foreach ( $add_comments_posts as $post ) {
delete_post_meta( $post->ID, 'add_comments_post_meta' );

The posts_per_pagevalue is used in the parameter -1to get all records.

Removing post metadata for a custom post type

If you only want to remove the metadata of a particular post type, replace the value $add_comments_post_argsin the code above with the following:

array( 'post_type' => 'add_comments_cpt', 'posts_per_page' => -1 );

add_comments_cptSubstitute the name of your type instead.


Leave a comment

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