Drush common snippets

Submitted by christophe on Tue, 27/12/2016 - 07:25

A collection of most used drush commands.
The full list of commands is documented on Drush Commands.

Fast site install for testing

Download Drupal core and install it in 3 commands.

# Download the latest dev release, creates a new directory
drush dl drupal-8.4.x
# Create a database
mysqladmin -u username -p create [db_name]
# Install, mysql://USER:PASSWORD@localhost/DBNAME
drush site-install standard --account-name=admin --account-pass=admin --db-url='mysql://[db_user]:[db_password]@localhost/[db_name]' --site-name=Dev84

The same, but with cloning from git then install with Composer. The benefit is that you can more easily update the core from the latest commits.

# cd in your docroot then clone the latest dev release
git clone --branch 8.4.x https://git.drupal.org/project/drupal.git .
composer install
mysqladmin -u username -p create [db_name]
drush site-install standard --db-url='mysql://[db_user]:[db_password]@localhost/[db_name]' --site-name=Dev84

Create a SQL dump

Drush aliases allows to make easy db sync between dev - staging - production.
If you have not them configured, drush sql-dump is still a fast option, it is preferable to the mysqldump command because you get a lighter dump version, without the cache tables.

# On the project docroot (e.g. on sites/default/files)
drush sql-dump > dump.sql
# Read it on your other environment, we have 
mysql -u user_name -p db_name < dump.sql
# Do not forget to remove it because your are exposing this file on the public docroot
rm dump.sql

Login as a user

# Login as user 1, by default
drush uli
# A user id to log in as
drush uli --uid=2
# A user name to log in as
drush uli --name=Username
# A user mail address to log in as.
drush uli [email protected]

List all enabled modules or themes

List all enabled modules

drush pm-list --type=module --status=enabled

List all enabled modules and themes without the core and print them as a list

drush pm-list --status=enabled --pipe --no-core

Delete all the nodes from a content type

Enable the Devel Generate module (submodule of Devel)

drush genc --kill --types=article 0 0

Update translations

With the core locale module enabled, check then update available translations

drush locale-check
drush locale-update

Debug

In some situations (e.g. migrations), it can be useful to print complex data structure from PHP in the terminal.

drush_print_r($myArray);

Tags

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.