Changing the Magento theme regularly is not recommended, but sometimes you have to replace it. For branding purposes, security, or performance, you have to change the Magento theme.
However, uninstalling a Magento theme is not a simple task. You must delete the theme directory and remove all references to the theme in the database. Similar to uninstall the Magento extension.
This tutorial will take a quick close look and follow the exact steps to uninstall a Magento 2 theme.
Note: Don’t forget to take the backup of the Magento store before making any significant changes. Backups help in case of data loss due to unfortunate events and reduce risks.
Uninstall a Magento 2 Theme
The process to follow to uninstall the Magento 2 theme depends on the process of installing it. If the theme is installed manually, then it will be removed manually.
#1 Uninstall a Magento 2 Theme Manually
Are you using a custom Magento theme?
Then you must have installed it manually by placing the themes files into the Magento root folder (app/design).
Here’s how you can remove it safely:
Step 1: Enable Maintenance Mode
Set the store on maintenance mode to quickly iterate the changes when you uninstall the Magento 2 theme.
Log in to the Magento 2 server. Enable the maintenance mode using the following commands;
$ php bin/magento maintenance:enable
You will need to be the file owner to execute these commands.
Step 2: Remove the Theme Directory From the Magento Filesystem
The manually installed Magento themes files are typically located in the app/design directory. You have to find the theme files’ path and delete the theme folder.
For example:
$ rm -rf <magento-root>/app/design/frontend/<vendor-name>
Step 3: Remove the Theme Record From the Magento Database
Uninstalling the theme files will leave behind the folders and references that need to be cleared.
Execute the following command to delete all records and references to the theme inside the store database:
$ mysql -u <user> -p -e "delete from <dbname>.theme where theme_path ='<vendor>/<theme>' AND area ='frontend' limit 1"
Replace the following placeholders before executing the command:
- <user>: Enter the Magento database username.
- <dbname>: Enter the Magento database name.
- <vendor>/<theme>: Enter the relative path to the theme directory.
Step 4: Clear Caches
Run the command to clear the Magento cache:
$ php bin/magento c:f
Step 5: Disable Maintenance Mode
Finally, disable the Maintenance Mode so the changes can be displayed.
$ php bin/magento maintenance:disable
In case of errors on the frontend, clear the cache of any other caching applications, such as Varnish or Redis.
#2 Uninstall a Magento 2 Theme Using Composer
There are slight variations in uninstalling a Magento 2 theme using Composer based on how Magento 2 is installed. You could uninstall Magento themes using CLI if you installed Magento with Composer.
If you cloned Magento’s Git repository, you need to remove the theme from its composer.json file before running the command.
Here’s how to uninstall a Magento 2 theme using Composer:
Step 1: Remove the Theme From the Magento composer.json File (GitHub Install Only)
Note: Follow this step if you’ve installed Magento by cloning its Git repository.
Enter the Magento file system and navigate to the Magento root directory.
You have to edit the Magento composer.json file. Pick a text editor and delete the line referencing the theme package.
Once you remove the reference to the Magento theme package from the ‘require’ section, use the following command to update the dependencies.
$ composer update
Now it is safe to run the Magento theme uninstall command as described in the next step.
Step 2: Run the Magento Theme Uninstall Command
Run the following command in the CLI:
$ php bin/magento theme:uninstall --backup-code --clear-static-content {theme path}
What the command does:
- Find whether the themes files exist at the defined path
- Verifies the theme is a Composer package
- Identifies the dependencies
- Check the absence of a virtual theme, and that theme is not in use
If everything checks out as expected, it will:
- Enable the maintenance mode
- Backup up the codebase – if the backup-code command is executed
- Remove the theme from the database tables
- Remove the theme from the codebase using composer remove
- Clear the cache files and generated classes
- Clear the static content if it is specified in the command
- Disable the maintenance mode
In case of dependencies?
One Magento theme can depend on the other Magento theme. In that case, deleting one theme will affect the different theme.
You have to resolve the dependency issue and rerun the uninstall command. Both Magento themes can be removed simultaneously:
$ php bin/magento theme:uninstall frontend/SampleCorp/SampleModuleTheme frontend/SampleCorp/SampleModuleThemeDepend --backup-code
Final Thoughts: How To Uninstall a Magento Theme?
Learning how to delete a Magento theme the right way is essential for cleaning space on the server and keeping the Magento store secure. Of course, there is more to Magento’s performance and security than uninstalling the unused themes.
However, no matter how you uninstall Magento 2 theme, check out the Knowledge base provided by the theme developers. Consult them if there are any associated extensions or dependencies to take care of.
I hope this quick tutorial helps you to uninstall a Magento theme safely. If there is any question or doubt, please leave it in the comment box.