168 Views

Comprehensive Guide to Magento 2 Useful Commands

As a Magento 2 developer, the Command Line Interface (CLI) is an indispensable tool for managing your projects. From installation to deployment, troubleshooting to optimization, CLI commands empower developers to streamline workflows efficiently. This guide compiles an extensive list of Magento 2 SSH/CLI commands, offering practical insights to simplify your day-to-day tasks.

1. Admin User Management

Managing admin users is critical for backend operations, especially when handling access or resolving locked accounts.

Create an Admin User

Use this command to create a new admin user with specific details:

bash

Copy code

php bin/magento admin:user:create –admin-user=’admin’ –admin-password=’admin123′ –admin-email=’admin@yourwebsite.com’ –admin-firstname=’John’ –admin-lastname=’Doe’

Unlock Admin User Account

If an admin account becomes locked due to incorrect login attempts, unlock it:

bash

Copy code

php bin/magento admin:user:unlock username

2. Setup and Installation

Magento setup commands are vital during installation or after adding new extensions.

Upgrade Setup

After installing new modules or making database changes, run:

bash

Copy code

php bin/magento setup:upgrade

Upgrade Setup Without Removing pub/static Files

To retain static content during the upgrade process:

bash

Copy code

php bin/magento setup:upgrade –keep-generated

Uninstall Magento Application

Completely remove the Magento installation, including database entries:

bash

Copy code

php bin/magento setup:uninstall

3. Cache Management

Cache management ensures optimal performance and quicker response times.

Clean Cache

Remove outdated cache entries without clearing all files:

bash

Copy code

php bin/magento cache:clean

Flush Cache

Flushes the entire cache storage, including third-party systems:

bash

Copy code

php bin/magento cache:flush

Enable/Disable Cache

You can enable or disable all caches or specific types:

bash

Copy code

php bin/magento cache:enable php bin/magento cache:disable cache_type

4. Static Content Deployment

Static content deployment is crucial for front-end operations like rendering CSS, JavaScript, and images.

Deploy Static Content

Generate static files for the Magento frontend:

bash

Copy code

php bin/magento setup:static-content:deploy

Deploy for Specific Languages

Deploy static content for a specific language locale, such as en_US:

bash

Copy code

php bin/magento setup:static-content:deploy en_US

Force Deploy

Force deployment for Magento 2.2.x or later versions:

bash

Copy code

php bin/magento -f setup:static-content:deploy

Exclude Themes

Exclude specific themes during deployment to save time:

bash

Copy code

php bin/magento setup:static-content:deploy en_US –exclude-theme Magento/luma –no-html-minify

5. Catalog Management

Efficiently manage your product catalog with these commands.

Resize Product Images

Resize and regenerate product image thumbnails:

bash

Copy code

php bin/magento catalog:images:resize

Remove Unused Product Attributes

Clean up unused attributes to enhance performance:

bash

Copy code

php bin/magento catalog:product:attributes:cleanup

6. Cron Jobs

Magento uses cron jobs to automate recurring tasks like email notifications and reindexing.

Install Magento Crontab

Add Magento-specific cron jobs to the server’s crontab:

bash

Copy code

php bin/magento cron:install –force

Run Scheduled Jobs

Manually trigger the execution of cron tasks:

bash

Copy code

php bin/magento cron:run

Remove Magento Crontab

Remove Magento’s crontab entries if no longer required:

bash

Copy code

php bin/magento cron:remove

7. Index Management

Reindexing ensures that Magento’s data is updated and searchable.

Reindex All Data

Reindex all Magento data to reflect changes:

bash

Copy code

php bin/magento indexer:reindex

Check Indexer Status

View the current status of all indexers:

bash

Copy code

php bin/magento indexer:status

Set Indexer Mode

Switch between schedule or realtime modes for specific indexers:

bash

Copy code

php bin/magento indexer:set-mode schedule catalog_product_category

8. Internationalization

Support global customers by managing translations and locales.

Collect All Phrases

Extract phrases for translation into a CSV file:

bash

Copy code

php bin/magento i18n:collect-phrases -o “path/to/translation.csv”

Uninstall Language Packages

Remove unnecessary language packs:

bash

Copy code

php bin/magento i18n:uninstall –backup-code language_package_name

9. Deployment Modes

Magento supports two modes: Developer and Production.

Set Developer Mode

Switch to Developer Mode for debugging and development:

bash

Copy code

php bin/magento deploy:mode:set developer

Set Production Mode

Switch to Production Mode for optimal performance:

bash

Copy code

php bin/magento deploy:mode:set production

Check Current Mode

View the current deployment mode:

bash

Copy code

php bin/magento deploy:mode:show

10. Dependency Injection

Compile Magento’s dependency injection to improve application speed:

bash

Copy code

php bin/magento setup:di:compile

11. Module Management

Manage Magento modules with ease.

Enable or Disable Modules

Activate or deactivate modules:

bash

Copy code

php bin/magento module:enable Vendor_Module php bin/magento module:disable Vendor_Module

Uninstall Module

Remove a module entirely:

bash

Copy code

php bin/magento module:uninstall Vendor_Module

Check Module Status

View all enabled or disabled modules:

bash

Copy code

php bin/magento module:status

12. Backup Management

Safeguard your Magento environment with regular backups.

List Available Backups

View all stored backups:

bash

Copy code

php bin/magento info:backups:list

13. Information Retrieval

Fetch useful details about your Magento installation.

List Currencies, Languages, and Timezones

Get supported configurations for currencies, locales, and timezones:

bash

Copy code

php bin/magento info:currency:list php bin/magento info:language:list php bin/magento info:timezone:list

14. Maintenance Mode

Enable maintenance mode for updates or debugging.

Enable Maintenance Mode

Activate maintenance mode for all visitors:

bash

Copy code

php bin/magento maintenance:enable

Whitelist Specific IPs

Allow access for specific IPs during maintenance:

bash

Copy code

php bin/magento maintenance:enable –ip=192.168.1.1

Disable Maintenance Mode

Turn off maintenance mode once updates are complete:

bash

Copy code

php bin/magento maintenance:disable

Conclusion

This comprehensive guide to Magento 2 CLI commands equips developers with the tools needed to maintain and optimize their stores effectively. Whether you’re managing admin users, deploying static content, or switching deployment modes, this cheat sheet will streamline your operations. By mastering these commands, you’ll enhance productivity, resolve issues faster, and ensure your Magento store performs at its best.

Bookmark this guide for quick reference, and elevate your Magento development experience!