How to uninstall a Magento 2 theme?

As a seasoned Magento 2 developer, I’ve encountered situations where I need to uninstall a theme to switch to a new one, troubleshoot issues, or simply clean up my installation.

In this article, I’ll walk you through the detailed steps I take to uninstall a Magento 2 theme. This requires deleting the theme directory and removing all references to the theme in the database.

If you have used composer for theme installation, you will have to perform steps listed under the composer heading.

First, backup your files and DB before uninstalling your theme. This will help mitigate any corruption or losses.

Manual uninstallation

If you installed your theme manually, carefully follow the steps below.

Enable Developer Mode

Before I start the uninstallation process, I enable developer mode to ensure that I can see any errors or issues that may arise during the process.

 Pro Tip
Most common problem with Magento2 broken stores is that uninstallation took place in production mode.

Let’s delete the everything in the code and metadata folder first.

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

I run the following command in my terminal:

php bin/magento deploy:mode:set developer

Now that the developer mode is enabled, let’s try and disable the theme.

Next, disable the theme that needs to be uninstalled. Log in to the Magento 2 Admin Panel and navigate to Content > Design > Themes. Select the theme that needs to be uninstalled and click the Disable button.

This disables the theme.

Remove theme files

Remove the theme files from the Magento 2 installation using FTP or SSH.

Navigate to the app/design/frontend directory and delete the folder with the theme that needs to be uninstalled.

Additionally, check the pub/static directory and remove any theme-related files. This step is essential to remove all theme-related assets and prevent any conflicts with future theme installations.

Remove theme references from the database

Now, let’s remove all references to the theme from the database.

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

where the user is your magento DB username, dbname is the name of the database and <Vendor>/<theme> is the relative path to your theme directory.

Clear cache

php bin/magento c:f

Compose uninstallation

If you used composer, use the steps below.

Navigate to the Magento root directory and run the following commands

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

This verifies the existence of the theme, the composer package and other dependencies. If the verification fails, the command exits. However, if there are theme dependencies, you will see errors like

Cannot uninstall <theme> because the following package(s) depend on it: <plugin>

If everything checks out – it will

  • Put the store in maintenance mode and begin a backup if you used the –backup-code
  • Removes the theme from the database tables and proceeds with removal from code using composer remove
  • Clears the cache and generated class. If you used –clear-static-content , it will also clean the generated static view files.

This will fully uninstall the Magento theme.