Apparently, now Magento 2 supports uninstall scripts that allow db schema modification when uninstalling a module (horay!!).
As explained in here this only works for module installed via composer.
(I hope it will work in the future for all modules, but that's a different issue).
Let's say I have a module called Testing_Demo.
This module does 3 things that I would like to be removed when uninstalling it.
- adds a table called
testing_demo. So I need to drop it. - adds a product attribute called
demo. So this needs to be removed - has some settings in
system->configurationthat might or might not be stored in the tablecore_config_data. All these settings have the pathtesting_demo/.... So these need to be removed also.
How should my module uninstall script look like?
-
I guess your uninstallation script should be more like a integration test to see if the removal of your extensions will f up the frontend or the schema relationsAnton S– Anton S2015年08月10日 13:09:21 +00:00Commented Aug 10, 2015 at 13:09
-
I cannot contradict you there. you are probably right, but how do I do that? :)Marius– Marius2015年08月10日 13:13:38 +00:00Commented Aug 10, 2015 at 13:13
-
I have no idea jet but in theory you should know if the data you collect can be dropped or not and therefore the uninstallation process should be a step by step guide that instructs a merchant to verify if the following process has effects on merchant liabilities on accounting , on clients etc . so the part that is purely technical is probably easy to traverse layouts and see if your extension is referenced , extended , others are depending etc parts that are uniform to all extensions but the business decisions behind are still up to merchant to decide and you can only point out conflictsAnton S– Anton S2015年08月10日 13:25:57 +00:00Commented Aug 10, 2015 at 13:25
-
the dependency should be handled before the uninstall so this is not my problem. Let's say I decided to remove the extension completely, and there is nothing depending on it.Marius– Marius2015年08月10日 13:34:11 +00:00Commented Aug 10, 2015 at 13:34
-
so you just need to narrow this down to plain dumping tables and data side of things?Anton S– Anton S2015年08月10日 15:39:45 +00:00Commented Aug 10, 2015 at 15:39
1 Answer 1
Searching the codebase for UninstallInterface gives \Magento\Setup\Model\UninstallCollector.
If you search for UninstallCollector then, you'll find that's used in \Magento\Setup\Console\Command\ModuleUninstallCommand. Particularly relevant:
$uninstalls = $this->collector->collectUninstall();
$setupModel = $this->objectManager->get('Magento\Setup\Module\Setup');
foreach ($modules as $module) {
if (isset($uninstalls[$module])) {
$output->writeln("<info>Removing data of $module</info>");
$uninstalls[$module]->uninstall(
$setupModel,
new ModuleContext($this->moduleResource->getDbVersion($module) ?: '')
);
} else {
$output->writeln("<info>No data to clear in $module</info>");
}
}
Put together, we can surmise:
- Your module should contain an
Uninstallclass at{module}\Setup\Uninstall.php. - This class should implement
Magento\Framework\Setup\UninstallInterface. - This class should have an
uninstallmethod containing any necessary logic. - The same objects and methods are available to you as in any setup or upgrade script.
So, here's your skeleton:
<?php
namespace \Custom\Module\Setup;
class Uninstall implements \Magento\Framework\Setup\UninstallInterface
{
/**
* Module uninstall code
*
* @param \Magento\Framework\Setup\SchemaSetupInterface $setup
* @param \Magento\Framework\Setup\ModuleContextInterface $context
* @return void
*/
public function uninstall(
\Magento\Framework\Setup\SchemaSetupInterface $setup,
\Magento\Framework\Setup\ModuleContextInterface $context
) {
$setup->startSetup();
// Uninstall logic here
$setup->endSetup();
}
}
Remove any tables, columns, or data using the appropriate methods. See \Magento\Framework\DB\Adapter\AdapterInterface, available as $setup->getConnection().
-
Thanks for the answer. I will test and come back with a result.Marius– Marius2015年08月10日 20:30:51 +00:00Commented Aug 10, 2015 at 20:30
-
@Marius you didn't mention whether it is working for you or not. Additionally I would like to know will this uninstall script runs directly or it will be triggered when we run module:uninstallAdnan– Adnan2017年04月17日 07:50:39 +00:00Commented Apr 17, 2017 at 7:50
-
1@Adnan. Yes. It worked. The script is called when running the console command do uninstall the module.Marius– Marius2017年04月17日 09:06:00 +00:00Commented Apr 17, 2017 at 9:06
-
@Marius, In case you have a team, what should do other developers? each of them locally should run command? Is any case to run it automatically with next pull?sergio– sergio2017年10月06日 07:53:44 +00:00Commented Oct 6, 2017 at 7:53
-
[Exception] Deprecated Functionality: Methods with the same name as their class will no t be constructors in a future version of PHP; Uninstall has a deprecated co nstructor in .../Setup/Uninstall .php on line 5Pini– Pini2018年06月24日 10:32:11 +00:00Commented Jun 24, 2018 at 10:32
Explore related questions
See similar questions with these tags.