Magento 2 Command List: Essential CLI Commands for Developers
Magento 2’s Command Line Interface (CLI) is a powerful tool for developers to manage installations, extensions, cache, and deployments. Below is a comprehensive guide to the most critical Magento 2 CLI commands, complete with explanations, use cases, and pro tips.
1. Setup Upgrade & Module Management
After installing a new module or extension, always run these commands to update the database schema and regenerate classes:
Key Commands
- php bin/magento setup:upgrade
- Updates the database schema.
- Warning: Removes pub/static files. Use –keep-generated to preserve them:
php bin/magento setup:upgrade --keep-generated
- php bin/magento module:status
- Lists all installed modules (enabled/disabled).
- Enable/Disable Modules
php bin/magento module:enable Namespace_Module php bin/magento module:disable Namespace_Module
When to Use
| Scenario | Command |
|---|---|
| New module installed | setup:upgrade |
| Debugging module issues | module:status |
| Disabling faulty modules | module:disable |
2. Static Content Deployment
Deploy static files (CSS, JS, images) for frontend themes:
Core Commands
- Basic Deployment
php bin/magento setup:static-content:deploy
- Language-Specific Deployment
php bin/magento setup:static-content:deploy en_US
- Theme-Specific Deployment
php bin/magento setup:static-content:deploy --theme Magento/luma
Pro Tips
- Use –no-html-minify to skip HTML minification for debugging.
- Exclude themes with –exclude-theme.
3. Cache Management
Magento’s caching system speeds up performance but often requires clearing during development.
Cache Commands
| Command | Purpose |
|---|---|
| php bin/magento cache:clean | Cleans cached files |
| php bin/magento cache:flush | Flushes all cache storage |
| php bin/magento cache:enable | Enables all caches |
| php bin/magento cache:disable | Disables all caches |
When to Clear Cache?
- After modifying XML layouts.
- After updating configuration.
- Before testing frontend changes.
4. Reindexing
Magento uses indexes for catalog, pricing, and search. Keep them updated!
Index Commands
- Reindex All
php bin/magento indexer:reindex
- Check Index Status
php bin/magento indexer:status
Index Modes
| Mode | Description |
|---|---|
| Update on Save | Real-time (slows performance) |
| Update by Schedule | Cron-based (recommended for production) |
5. Deployment Modes
Magento 2 runs in three modes:
Mode Commands
- Developer Mode (Debug-friendly)
php bin/magento deploy:mode:set developer
- Production Mode (Optimized)
php bin/magento deploy:mode:set production
Comparison
| Feature | Developer Mode | Production Mode |
|---|---|---|
| Static File Generation | On-demand | Pre-generated |
| Error Verbosity | Detailed | Minimal |
6. Advanced Commands
- Dependency Injection Compilation
php bin/magento setup:di:compile
- Uninstall Modules
php bin/magento module:uninstall Namespace_Module
7. Troubleshooting Tips
- Broken Frontend? Run:
php bin/magento setup:static-content:deploy php bin/magento cache:flush
- Database Sync Issues? Reindex and upgrade:
php bin/magento indexer:reindex php bin/magento setup:upgrade
Final Thoughts
Mastering Magento 2 CLI commands streamlines development and maintenance. Bookmark this guide for quick reference!
Further Reading: