How to Uninstall Magento 2 Modules and Restore Magento 2 Backups (Rollbacks)

- Magento 2

Uninstall Magento 2 Extensions; Restore Magento 2 Rollbacks

In this Magento 2 tutorial, I explain how to uninstall Magento 2 modules. You have several uninstallation options, such as removing the modules’ code, database data, and database schema. I highly recommend you to create backups before doing any changes, because you will leave a chance to recover the data if something goes wrong.

Magento 2 tutorials, features, and rumours on Firebear

Moreover, uninstall modules only if you are not going to use them. If you are not certain whether an extension will be useful or not, disable it instead.

You should also take into consideration that the following command checks dependencies from composer.json. Therefor, uninstalling a module that is not defined in composer.json, the command uninstalls it without checking for dependencies. By using the command, you will not remove the code of the module from the Magento 2 file system. Use file system tools in order to do this. For instance, you can rely on rm -rf <path to module>.

Prerequisites

Remember! You should be logged in to the Magento server as a user with permission to write to the file system. For instance, you can be logged as a web server user.

By adding <your Magento install dir>/bin to your Magento 2 system PATH, you will be able to run commands from all directories. Please note that shells have differing syntax, so I propose you to check a reliable source for additional information. For example unix.stackexchange.

In case of CentOS, bash shell has the following look: export PATH=$PATH://var/www/html/magento2/bin.

Additionally, there are a few other ways to run commands. Firstly, cd <your Magento install dir>/bin – run them as ./magento <command name>. Secondly php <your Magento install dir>/bin/magento <command name>. Where <your Magento install dir> is a subdirectory of the web server’s docroot.

How to Uninstall Magento 2 Modules

To uninstall Magento 2 modules, use the following command:

where {ModuleName} is <VendorName>_<ModuleName>. Don’t know how to get a list of Magento 2 module names? Simply run magento module:status.

As for tasks performed by the module uninstall command, they are listed below:

1. First of all, it verifies if specified modules exist in the code base and simultaneously checks if packages are installed by Composer. Thus, the command works only with Magento 2 extensions defined as Composer packages.

2. The second important task consists in checking for dependencies. The command terminates if there are any. Taking this into account, you can easily uninstall all Magento 2 modules at the same time or remove the depending extensions first.

3. Then, the command requests confirmation to proceed, and puts the store in maintenance mode.

4. The next step is about processes the following options:

  • –backup-code – this option is utilized to back up the Magento 2 file system. Please note that var and pub/static directories are excluded. Backup file name and its location are var/backups/<timestamp>_filesystem.tgz
  • –backup-media – creates a backup for the pub/media directory.  Backup file name and its location are var/backups/<timestamp>_filesystem_media.tgz
  • –backup-db – the name of this option speaks for itself: it backs up the Magento 2 database. Backup file name and its location are var/backups/<timestamp>_db.gz

6. For the specified –remove-data option, the command removes the database schema and data from the Uninstall classes of the module. Additionally, for each module specified to uninstall, it invokes the uninstall method from the Uninstall class. It is extremely necessary to mention that this class must inherit from UninstallInterface.php.

7. The command also removes the specified modules from the setup_module database table, as well as the module list from the deployment configuration.

8. Moreover, it removes code from the codebase with the help of the composer remove option. Uninstallation always runs this option; at the same time –remove-data removes database data and schema defined by the Uninstall class of the module.

9. Of course, the uninstall command cleans the cache.

10. And updates generated classes.

11. For the specified –clear-static-content option, it clears generated static view files.

12. The last task is taking the Magento 2 store out of maintenance mode.

Let’s look at the following example. You’ve tried to uninstall a module (Magento_SampleMinimal) that another module (Magento_SampleModifyContent) depends on. As a result, you should see the following:

To solve the problem, you can uninstall both modules. But don’t forget to back up the Magento 2 app/code file system, database tables, and pub/media files. At the same time, it is necessary not to delete the module’s database schema or data. Due to all these requirements, you should get the following command:

After running it, you should get the following message:

Uninstalling Magento 2 Extensions

You will always get an errors if uninstalling a module with a dependency on another one. To solve the problem, uninstall both Magento 2 modules.

How to restore backups (Magento 2 rollback)

If something went wrong or you just need to restore the Magento 2 codebase to its previous state, utilize the below command:

Where <filename> is the backup file name – the file is situated in <your Magento install dir>/var/backups. Run the following command to display a list of backup files: magento info:backups:list

Please note that the rollback command deletes the specified files as well as the database before restoring them. Thus, the –media-file option removes media assets under pub/media before restoring an appropriate rollback file. So, make sure no changes to the file system or database have been made, and only then use the command.

The rollback command does the following:

1. It runs maintenance mode.

2. Then the command verifies the backup name.

  • In case of a code rollback file, it checks if the rollback destination locations are writable, ignoring the pub/static and var folders; deletes files and directories under the Magento 2 installation directory; and finally extracts the archive file to appropriate locations.
  • For a database rollback file, the rollback command drops the entire Magento database and then restores it from the database backup.
  • If a media rollback file has been specified, the command verifies if the rollback destination locations are writable; removes everything from pub/media; and extracts the backup to appropriate locations.

3. Now, it can takes the store out of maintenance mode.

Let’s see a little example. To restore a code backup, a file system backup, run the following commands. Don’t miss the order:

First of all you should get a list of your Magento 2 backups. Use the following command:

Now, you should restore the 1733876617_filesystem.tgz backup file:

The following message should be displayed:

Restoring Magento 2 Rollbacks

You can check the official documentation related to the uninstallation Magento 2 modules and restoring rollback (backup) files here.