Answers

We’ve gathered common questions about Magento and are happy to share our answers.

Answers
Staylime’s related service

How to uninstall a Magento 2 theme?

To uninstall a manually installed Magento theme, you must delete the theme directory and remove all references to the theme in the database. For a theme that was installed using Composer, you must use another way.

Let’s take a closer look at the exact steps required to uninstall a Magento theme for your store in both ways.

Note: You should take a backup of your database and filesystem before uninstalling your theme. This will help mitigate the risk of data loss due to an incorrect uninstallation.

1. Manual uninstallation

If your theme was installed manually into the Magento directory, follow these steps to uninstall it manually from your server.

Step 1: Set an alternate theme

Before you uninstall a theme from your Magento store, make sure it’s not currently applied for any store views.

To do this, navigate to Content > Design > Configuration in your Magento admin panel. Then select the ‘Edit’ option that corresponds to any store view where the theme might be active. 

Ensure the theme you want to uninstall isn’t applied on the selected store view and that you’ve set a replacement theme before moving on to the next step.

Step 2: Enable developer mode

This is an often overlooked yet important step, leading to incorrect uninstallations and broken websites. If your store is operating in the production mode, you need to change it to the default or developer mode before you proceed.

To do this, navigate to the Magento root directory and run the following commands in the terminal:

$ rm -rf generated/metadata/* generated/code/*

This will remove any code generated previously and will prevent errors during the uninstallation process.

Next, execute the following command:

$ php bin/magento deploy:mode:set developer

Optionally, you can put your store in maintenance mode to temporarily disable access to frontend for site visitors. To that end, run the following command in your terminal:

$ php bin/magento maintenance:enable

Step 3: Remove the theme directory

Next, navigate to the directory where the theme is installed. The directory path is usually <Magento root directory>/app/design/frontend/ for manually installed themes.

Once there, remove the theme.

Step 4: Remove theme record from the database

Next, remove all records and references to the theme from the site database by running the following command:

$ mysql -u <user> -p -e "delete from <dbname>.theme where theme_path ='<Vendor>/<theme>' AND area ='frontend' limit 1"

Make sure you replace the following values before running the command in your terminal:

<user>: your Magento database username
                            <dbname>: your Magento database name
                            <Vendor>/<theme>: relative path to the theme directory

Step 5: (Optional) Clear all cache files

$ php bin/magento c:f

If you’re using Varnish, you can manually clear all cache files to avoid any stale cache from being served post uninstallation by restarting the service.

2. Composer uninstallation

The flow for uninstalling is different, depending on the way that Magento instance was installed.

Uninstall a theme package if Magento was installed using Composer

You could uninstall it by following these steps:

Navigate to the Magento root directory as the Magento filesystem owner and run the following command in the terminal:

$ php bin/magento theme:uninstall --backup-code --clear-static-content {theme path}

This command performs the following tasks:

  1. Verifies that the theme exists at the location defined at {theme path}.
  2. Verifies that the theme is a Composer package.
  3. Checks for dependencies, verifies the absence of a virtual theme, and that the theme isn’t currently being used.

If at any of these points the verification fails, the command terminates immediately. For example, if your theme has dependencies, you’ll see the following error message after the command terminates:

Cannot uninstall <themename-1> because the following package(s) depend on it:
                            <themename-2>

If all checks complete successfully, it will proceed as follows.

  1. Puts the store in maintenance mode and begins a backup of the codebase if the --backup-code command has been used.
  2. Removes the theme from the theme database table and then proceeds to remove it from the codebase using composer remove.
  3. After this, it cleans the cache, clears generated classes, and if --clear-static-content has been specified, it cleans any generated static view files.

After the command is successfully executed, your Magento theme will be uninstalled.

Uninstall a theme package if Magento was installed by cloning the repository

To uninstall a theme Composer package if your Magento instance was installed by cloning the Git repository, you can also uninstall it using a CLI command. However, you must first remove it from the list of dependencies.

Take the following steps:

Open the <Magento root dir>/composer.json file. Find a line with a reference to theme package and delete it. The reference would look like following:

"require": {
                             ...
                            "<vendor>/<theme-name>": "<version>"
                            },

To update the project dependencies, run:

$ composer update

Use the magento theme:uninstall CLI command as was described previously.

Uninstall a theme extension

If the theme was installed as an extension, you can uninstall it the same way that theme Composer packages are uninstalled.