Can a plugin deactivate and delete itself once installed and activated?

I have a unique plugin whose purpose is to set up a new WordPress site with a theme, default widgets, default plugins, custom menus, pages, posts, etc.

The plugin does everything it needs to do when activated and never runs again.

Read More

I’m looking for suggestions on how I might, as the last step in the activation routine, deactivate and delete the plugin.

Any help, suggestions, or example references appreciated.

Related posts

Leave a Reply

2 comments

  1. You can deactivate it with:

    deactivate_plugins( basename( __FILE__ ) );
    

    I don’t think you can delete it. It would be a big security risk, IMO. But if the folder has the correct (but insecure) permissions, you could use the PHP function rmdir

  2. You can use WordPress’s “filesystem api” for deleting files, same as wp core can. It’s a neat little system that asks politely for ftp user credentials if it doesn’t have permission to do it itself transparently. (I don’t think it’s really a security issue, since wordpress has install, auto update, and removal of plugins capabilities out of the box.)