Drush: A Guide to Essential Commands for Speeding Up Your Drupal Workflow

By vishal_prasad
Essential Commands for Speeding Up Your Drupal Workflow

Drush, or Drupal Shell, is a simple command line interface tool that interacts with the Drupal site. It allows you to perform tasks directly from the command line on a Drupal site, especially when you don’t have UI access. Drush is a powerful tool used by developers and DevOps for site-related operations. This article will discuss some practical and most commonly used Drupal Drush commands to speed up your workflow.

Drush Command-List

Drush commands are handy for accessing and editing website content. Here is a list of Drush commands that you should know:

1. Drush core:status (drush status, drush st): 

Have you ever wondered how to check the status of your Drupal site? There are many ways to do so, such as using the status report from the UI. However, what if you find yourself in a situation where you don't have UI access to the site, and you still want to know the status of your Drupal site, including details like the PHP version, MySQL version, database name, port, and other insights? 

There is one simple command for this: drush status

drush status

2. Drush user:login (drush uli): 

Imagine a scenario where you are working on a Drupal project in a team. You fork the project to your local machine and perform the required drupal workflow  setup. 

As a developer, you need admin access to the site even in your local environment, but the team failed to create an admin account for you during the database import.

Fortunately, there is a command that allows you to obtain admin access to the site easily and without hassle. This command is drush uli, which generates a one-time admin login link for you.

This is the type of output you get after running this command. 

Drush user:login


3. Drush cache:rebuild (drush cr):  

If you have made some Front-end related CSS or JS changes or other modifications that are not reflected on the site. It indicates that you need to clear the cache of the site. Depending on your requirements, you can either rebuild all the cache or clear the cache from a specific bin. For development, you can also disable caching on your site. 

(For more information on drupal workflow , you can also refer to these links: Disable cachingDisable Twig Caching.)

4. Configuration-related commands:

For configuration, you can use the following commands:

  • drush config:export (drush cex): Use this command to export the current configuration of your drupal site.
  • drush config:import (drush cim): Use this command to import the configurations you previously exported. However, you should use it with caution, as any UI-related configuration changes will be overwritten by this command, and there is a chance that you may lose the changes you had already made. You can also set a particular configuration value without disturbing the site's other configurations.
  • drush config:set (drush cset <config-name> <value>): Use this Command to set the configuration . values explicitly from the terminal.
     

For example:

drush config:set system.site name MySite

This command will alter the site name in the configuration object named system.site

  • drush config:delete (drush cdel): If a configuration is causing conflicts on your site and hindering the successful import or export of configurations, you can delete that specific configuration using the command "drush cdel <config-name>".

Similarly, there are commands to edit any configuration ("drush cedit <config-name>") or read any configuration ("drush cget <config-name>")

5. Drush devel:services (drush dcs):

Enable the devel module if searching for a specific service name in your code. You can retrieve the service names on your Drupal site using this command.

6. Drush user:information (drush uinf): 

This command will provide you with the profile information of a specific user specified by the arguments. It is a highly useful Drush command for quickly retrieving details about user accounts without needing to access the Drupal admin interface. 

For example, you might need to check user roles, statuses, email addresses, or other profile information for troubleshooting or administrative purposes. You should enter the following:

drush user:information --mail=someguy@somegal.com

This command will give the user's information using the email ID someguy@somegal.com. The output will include details such as the user's UID, status (active or blocked), roles assigned, last access time, and more.

Similarly, you can use other identifiers to fetch user information:
drush user:information --uid=123

7. Drush user:create (drush ucrt): 

This drush Command will let you create a new user on your site by providing their details directly from the command line, as shown below. You just need to provide the basic user details of the user to be created.

Drush user:create

8. Drush user:role:add (drush urol): 

This command will let you add a user role to a user account, as shown below

Drush user:role:add

9. Drush watchdog:show (drush wd-show, drush ws): 

This command displays the 10 most recent log messages on the command line. It is useful for retrieving information logged by various processes running on the site and debugging any errors.

Drush watchdog:show

10. Drush gen: 

This command allows you to generate the basic code template for controllers, plugins, and many other components. 

For example:

  • drush gen controller: This command will generate the controller inside the /module/src/ folder with a basic code template to begin with.
  •  
drush gen
  • This command also accepts some arguments to generate different code blocks.

  • drush gen plugin:block: This command will generate the block plugin for you, complete with a basic code template. It includes annotations for the block and the required methods to be implemented when extending the BlockBase. 
  •  
drush gen plugin:block
  •  
  • drush gen module: This command will generate the entire module for you, including the basic info file and the plugins, controllers, services, menus, and routers (optional) that you desire to include inside your module.
  •  
drush gen module
  •  

  • 11. Drush site:install (drush si): 

If you are installing a new Drupal site or have completely messed up your existing website, you can use the command drush si to install the Drupal site, resetting all configurations. This Drush command is extremely powerful as it reinstalls the Drupal core, sets up the database, installs required modules, and applies default configurations. It can be a lifesaver when you're starting fresh on a new project or need to revert to a clean slate due to extensive issues.

When running the drush site:install command, you can specify various options to tailor the installation process to your needs. For example, you can set the site name, email, admin username, and password in one go. Additionally, you can include profiles and installation tasks to be executed during the process.

Here’s an example of using drush site:install with additional parameters:

drush site:install standard --account-name=admin 
--account-pass=admin --site-name="My Drupal Site" 
--db-url=mysql://dbuser:dbpass@localhost/dbname

In this example:

  • standard specifies the installation profile.
  • --account-name sets the username for the admin account.
  • --account-pass sets the password for the admin account.
  • --site-name sets the name of the site.
  • --db-url specifies the database connection details.


This level of customization makes the drush site:install command a robust tool for developers and system administrators who need to deploy Drupal sites quickly or recover from critical issues.

Using this command cautiously is important, as it will reset all configurations and the database. This action cannot be undone, so ensure you have backups of important data and configurations before proceeding. For instance, before running drush si, you might want to export your current configurations using drush config:export and back up your database.

By leveraging the drush site:install command, you can automate the setup of development, staging, or production environments, making it a crucial part of your Drupal workflow. This command helps maintain consistency across different environments, reducing the chances of configuration drift and ensuring that your Drupal site operates smoothly and as expected.

12. Drush sql:cli (drush sqlc): 

This command will open an SQL command line interface to perform database-related operations. 

Drush sql:cli

It is helpful when you don’t have PHPMyAdmin installed or only have server access to the site and want to obtain information about the database tables on your site. Similarly, you can also export or import a database to your site using the following command:

drush sql:cli < example.sql 

The "example.sql" is the database file name to be imported.

drush sql:dump > example.sql 

The "example.sql" is the database file name where the site database will be dumped.

13. Drush updatedb (drush updb): 

If you have written logic to update the database in the module’s install file, you need to execute the drush updb command to ensure those updates are reflected in the database. It will run all the pending database schema or data-related updates on your site's database.

14. Drush php:eval (drush ev, drush eval): 

This Command will let you execute the php code snippets directly from the command line as shown below.

drush php:eval

15. Drush php: 

If you have a long PHP script to execute, you might not want to execute each line one by one using drush php:eval. The drush php command opens a PHP shell command line interface to run PHP and Drupal code seamlessly. This Drush command is particularly useful when you need to execute complex scripts that involve multiple steps or when you need an interactive environment to test and debug PHP code in real time.

For instance, if you're developing a custom module and need to test various PHP code snippets, you can use the drush php command to enter an interactive shell. This allows you to experiment with different functions, classes, and methods within the context of your Drupal site without constantly refreshing the page or writing temporary scripts.

Moreover, the drush php command can be invaluable for performing advanced debugging and troubleshooting tasks. You can quickly test changes, validate the impact of different configurations, or run maintenance scripts directly from the command line. This Drush command eliminates the overhead of navigating through the Drupal UI, making your development process more efficient and streamlined.

Here’s how you can start the PHP interactive shell using Drush:

drush php

Conclusion: 

In conclusion, mastering a range of Drush commands can significantly enhance your efficiency and effectiveness as a Drupal developer. By automating repetitive tasks, facilitating rapid troubleshooting, and streamlining complex processes, Drush allows you to focus more on what truly matters – building robust and dynamic web experiences. Whether managing site configurations, handling content, or optimizing performance, the right Drush commands can transform your workflow, saving you valuable time and effort. Embrace these Drush tools, and you'll find yourself navigating your Drupal projects with newfound agility and confidence.
 

share on :

Profile picture for user vishal_prasad
vishal_prasad
An enthusiastic personality with interest in designing, and problem solving, having decent knowledge of some designing tools and programming languages like C, java, PHP and CMS.