Drupal Tutorails: Drush Complete Guide

Drush (Drupal Shell) is a command-line shell and scripting interface for Drupal. It provides a suite of commands for managing your Drupal sites, making tasks like module updates, database backups, migrations, and other maintenance tasks much easier and faster. Drush can be used to perform nearly any administrative task for Drupal from the command line.

Installing Drush

Step 1: Install Composer

Composer is a dependency manager for PHP, and it’s required to install Drush.

# Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Step 2: Install Drush Globally

Using Composer, you can install Drush globally:

# Install Drush globally
composer global require drush/drush

# Make sure Composer's global bin directory is in your PATH
echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Alternatively, you can install a specific version of Drush globally:

# Install Drush 10 globally
composer global require drush/drush:10.*

Step 3: Verify Drush Installation

To verify that Drush is installed correctly, run:

drush --version

You should see output indicating the version of Drush that is installed.

Using Drush

Drush commands follow a common pattern:

drush [command] [options] [arguments]

Here are some of the most commonly used Drush commands:

Step 1: Clear Cache

Clearing the cache is a frequent task during development.

drush cache-clear all

In newer versions of Drush, use:

drush cr

Step 2: Download and Enable a Module

To download and enable a module, use:

drush dl module_name
drush en module_name

For example, to download and enable the Pathauto module:

drush dl pathauto
drush en pathauto

Step 3: Update Drupal Core and Modules

To update Drupal core and all modules, run:

drush pm-update

To update only Drupal core, use:

drush pm-update drupal

Step 4: Run Database Updates

After updating modules or core, you often need to run database updates:

drush updatedb

Step 5: Backup and Restore Database

To create a backup of your database, use:

drush sql-dump > drupal-backup.sql

To restore the database from a backup:

drush sql-cli < drupal-backup.sql

Step 6: User Management

To create a new user:

drush user-create username --mail="email@example.com" --password="password"

To block a user:

drush user-block username

To unblock a user:

drush user-unblock username

To assign a role to a user:

drush user-add-role "role_name" username

Step 7: Watchdog Log

To view the watchdog log (recent site logs):

drush watchdog-show

To clear the watchdog log:

drush watchdog-delete all

Step-by-Step Example: Setting Up a New Drupal Site with Drush

Step 1: Install Drupal using Drush

Create a new Drupal project using Composer:

composer create-project drupal/recommended-project my_drupal_site
cd my_drupal_site

Step 2: Set Up a Database

Create a new database for your Drupal site:

mysql -u root -p
CREATE DATABASE drupal;
GRANT ALL PRIVILEGES ON drupal.* TO 'drupaluser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EXIT;

Step 3: Install Drupal Site

Run the site installation command:

drush site-install standard --db-url='mysql://drupaluser:password@localhost/drupal' --site-name='My Drupal Site' --account-name='admin' --account-pass='adminpassword'

Step 4: Configure Drush for Your Site

Create an alias for your site to make Drush commands easier to run:

drush site-alias @my_drupal_site --root=/path/to/my_drupal_site --uri=http://localhost

Now you can use the alias to run Drush commands:

drush @my_drupal_site status