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->configuration
that 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?
Best Answer
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:Put together, we can surmise:
Uninstall
class at{module}\Setup\Uninstall.php
.Magento\Framework\Setup\UninstallInterface
.uninstall
method containing any necessary logic.So, here's your skeleton:
Remove any tables, columns, or data using the appropriate methods. See
\Magento\Framework\DB\Adapter\AdapterInterface
, available as$setup->getConnection()
.