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.

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.

2 Answers 2


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

  • thanks for the help. The plugin is designed to be uploaded once and ran once, then it sets a flag so it cannot run again. Curious about security risk of allowing the plugin to self destruct once its done.
    – Scott B
    Commented May 9, 2011 at 18:59
  • I think it's bad security policy allow a plugin to delete files in your server. But if you set write permissions to that particular plugin php, you can delete it with the unlink(__FILE__); function. Remember to deactivate before deletion
    – MZAweb
    Commented May 9, 2011 at 19:16
  • I think I understand, but just wanted to clarify, the plugin is deleting itself, not other files. Maybe the same difference as far as your premise is concerned. It adds some files to the site, inserts some custom settings, creates some widgets, etc, then it deactivates and deletes ITSELF.
    – Scott B
    Commented May 9, 2011 at 20:13
  • All PHPs in apache run under the same user. If you give that user write permission for a file, any PHP could delete or modify it. But as i said, if you set permissions (eg: 777) to your plugin, I didn't test it, but I think you'll be able to delete it.
    – MZAweb
    Commented May 9, 2011 at 20:26
  • are you planning on sharing the plugin?
    – anu
    Commented May 11, 2011 at 9:35

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.