I am creating a module and I am progressing it. I am new to Magento 2 so there are a lot of things that I am not yet familiar too. As a module creator, I am curious how to run an uninstall script when someone uninstalled your module via command line.

I know I can execute the module uninstall command like this:

php bin/magento module:uninstall -r Vendor_Module

2 Answers 2


Magento Setup Uninstall script

The below page in the Magento DevDocs explains the process of module uninstallation. It explains the steps that are taken and one of those steps is running Uninstall scripts of the modules:


  1. If --remove-data is specified, removes the database schema and data defined in the module's Uninstall classes.

    For each specified module to uninstall, invokes the uninstall method in its Uninstall class. This class must inherit from Magento\Framework\Setup\UninstallInterface.

So this indicates for a module to run a script at uninstall, the module should be uninstalled using the --remove-data option, or shorthand -r. So if the users of your module do not use that option, the script won't run.

For a "Company MyModule" module, the script should be located at app/code/Company/MyModule/Setup/Uninstall.php or vendor/company/mymodule/Setup/Uninstall.php and must implement the \Magento\Framework\Setup\UninstallInterface interface. It could look like this:

namespace Company\MyModule\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;

class Uninstall implements UninstallInterface
    public function uninstall(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        // Uninstall schema and/or data...

Composer Package Uninstall event script

If your module is a composer package (residing in vendor/), then the Magento module uninstaller will run composer remove to remove the package files that were installed into the vendor dir.

Now, composer has a mechanism to run scripts on certain events, including package removal/uninstall. Unfortunately for module vendors, only scripts configured in the root composer.json will be ran. So if you have scripts configured like below in your root composer.json (root of your Magento project), then these will be triggered whenever a module will be uninstalled through the Magento bin/magento module:uninstall command:

    "scripts": {
        "pre-package-uninstall": "Company\\MyModule\\Setup\\Uninstall::composerUninstall",
        "post-package-uninstall": [
            "php -r \"copy('some-dir/some-file.php.backup', 'some-dir/some-file.php');\""


  • 1
    This is what I needed! Thank you @7ochem!
    – Ner
    Jan 11, 2018 at 0:09
  • 2
    It could be worth mentioning here that the Module being uninstalled should have been installed from composer. Manually Extracted modules wont work here. Apr 27, 2019 at 8:29
  • It just work only when we install module via composer
    – HaFiz Umer
    Nov 4, 2019 at 7:17

run : php bin/magento module:uninstall -r Vendor_Module

  • As how I read it, the question is not how to uninstall a module, but how to make a script run when the module is being uninstalled. @Ner Maybe you should clarify your question a little bit
    – 7ochem
    Jan 10, 2018 at 14:04
  • @7ochem is right. I know how to execute the module uninstall command. What I wanted to know is how could I run a script when uninstall command is called.
    – Ner
    Jan 10, 2018 at 15:07

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.