The most useful drush commands (Drupal 7)

Here I will try to maintain a drush cheat sheet with the list of drush commands I use most often. The full and short version of some commands are used interchangeably.

Ad: Are you interested in trying DigitalOcean hosting? Sign up via this link and get $100 in credit for free.

See information and status of the site

drush status

Clear the cache

drush cc all

Clear the cache, even if Drupal is broken

drush sql-query "DELETE FROM cache"

sql-query executes SQL queries in the database where Drupal is installed.

Download the last recommended release of 2 modules

drush dl module1 module2

Download a -dev version of a module

drush dl views-7.x-3.x --package-handler=git_drupalorg

Seems that adding --package-handler=git_drupalorg is not needed anymore.

Download the git HEAD version of a module

Short answer: drush does not do that.
Long answer: See http://drupal.stackexchange.com/q/62934/10086

Enable a module

drush en name_of_module -y

-y skips the confirmation question. Some drush commands may miss the -y part, a workaround for this that always works is putting it before:

drush -y en name_of_module

If the module is missing and its name matches a project name, drush will automatically download the module from drupal.org.

Disable a module

drush dis name_of_module

Disable a module, even if Drupal is broken

drush sql-query "UPDATE system SET status = '0' WHERE name = 'module_name'"
drush sql-query "DELETE FROM cache_bootstrap WHERE cid = 'system_list'"

Source: https://drupal.org/node/157632

Uninstall a module

drush pm-uninstall module_name -y

See if a module is available

drush pm-list | grep module_name

See if a module is enabled

drush pm-list --status=enabled | grep module_name

See all contrib and custom modules that are enabled

drush pm-list --type=Module --no-core --status=enabled

Update a module

drush up module_name

Update Drupal

drush up drupal

Update all contrib modules and Drupal

drush up

Run update.php

drush updb

Send an e-mail

drush php-eval "print mail('[email protected]', 'Subject', 'Message', 'From: [email protected]');"

Delete a field

drush field-delete fieldname

Delete an instance of a field

drush field-delete fieldname --bundle=article

Manually delete a field and all its data (not recommended)

drush sql-query "DELETE FROM field_config WHERE field_name = 'fieldname'"
drush sql-query "DELETE FROM field_config_instance WHERE field_name =' fieldname'"
drush sql-query "DROP TABLE field_data_field_fieldname"
drush sql-query "DROP TABLE field_revision_field_fieldname"

Set a password for a user

drush upwd --password="asdf" admin

Block a user

drush user-block joe

Log all users out

drush sql-query 'TRUNCATE TABLE sessions;'

Log out a specific user

drush sql-query 'DELETE FROM sessions WHERE uid = 2;'

Get a list of detaills for all users (name, user id, roles, email and status)

drush uinf $(drush sqlq "SELECT GROUP_CONCAT(name) FROM users")

Get a one-time login link for the Administrator user

drush uli

Run cron

drush cron

Run Ultimate Cron

drush cron-run

Set a variable

drush vset variable_name 'value'

Get the value of a variable

drush vget variable_name

See last logged events (watchdog)

drush ws

See logged events in real time

drush ws --tail

Run a php script with Drupal bootstrapped

drush scr --uri=domain.com script.php

Use --uri to specify the hostname. Use --root to specify the site directory (in cron, for example):

/usr/bin/drush --root=/var/www/drupal scr /var/www/drupal/sites/all/scripts/example.php >>/root/logs/scripts/example_log.txt 2>>/root/logs/scripts/example_errors.txt

Run custom code

drush eval "variable_set('foo', 'bar');"

Repopulate database tables used by menu functions

drush eval "menu_rebuild();"

See https://api.drupal.org/...

Rescan all code in modules or includes directories, storing the location of each interface or class in the database

drush eval "registry_rebuild();"

Useful when moving installed modules. See http://drupal.stackexchange.com/q/17657/10086
If it does not work because Drupal cannot bootstrap, do it with Registry Rebuild:

drush dl registry_rebuild
drush rr

Update a Feature with database changes

drush fu feature_name

Revert a Feature, update the database to match the code

drush fr feature_name

Revert all features

drush features-revert-all -y

Enable maintenance mode

drush vset -y maintenance_mode 1

Disable maintenance mode

drush vset -y maintenance_mode 0

Open a MySQL console logged in

drush sql-cli

Import a backup of the database

drush sql-drop; drush sql-cli < dump.sql

Import a compressed backup of the database

drush sql-drop; zcat dump.sql.gz | drush sql-cli

Export a backup of the database

drush sql-dump | gzip --stdout > $(date +\%Y-\%M-\%d-\%H-\%M-\%S).sql.gz

Remove all database tables (empty the database)

drush sql-drop

Useful before importing a database backup.

See all drush aliases

drush site-alias

Use an alias

drush @aliasname command

Flush image styles

drush image-flush all

Install Drupal

drush site-install

Generate random content (with Devel and Devel Generate modules)

sudo drush generate-content 20 --types=page

List available content types

drush php-eval "print_r(array_keys(node_type_get_types()));"

Source: https://drupal.stackexchange.com/a/174217/10086

Delete all content of specific content types (with Devel and Devel Generate modules)

sudo drush generate-content 0 --kill --types=facebook,instagram,tweet

Alternatives modules are Migrate (drush migrate-wipe) and Delete all.

Delete a content type

drush php-eval "node_type_delete('job_posting');node_types_rebuild();menu_rebuild();"

Delete a node (e.g. node id 34)

drush php-eval "node_delete_multiple(array(34));"

Multiple nodes:

drush php-eval "node_delete_multiple(array(34, 35));"

Delete a node by path or URL (e.g. http://localhost/about-us)

drush sql-query "SELECT source FROM url_alias WHERE alias = 'about-us'"

(if there is a node with that path, you'll get a short url like node/123)

drush php-eval "node_delete_multiple(array(123));"

Create a boilerplate for a new module

drush mb my_module menu cron --write --name="My module"

See Module Builder.

Clear all webform submissions

drush php-eval '$nodes = node_load_multiple(array(), array("type" => "webform")); foreach ($nodes as $n) { print $n->nid . "\n"; }' | xargs -n1 drush -y webform-clear

Run sanitization operations (run only in non-production databases)

drush sql-sanitize

Note that this also deletes all webform submissions. You may implement hook_sql_sync_sanitize() in your custom modules to clean specific data.

Same as above, but also setting a default login password for all users

drush sql-sanitize --sanitize-password='newpassword'

Same as above, but also sanitizing all user emails

drush sql-sanitize --sanitize-password='newpassword' --sanitize-email

Clear all webform result submissions of node id 3

drush webform-clear 3

Delete all entityform submissions

drush php-eval "entity_delete_multiple('entityform', db_query('SELECT entityform_id FROM {entityform}')->fetchCol());"

See https://www.drupal.org/node/2905278

Compare 2 Drupal databases with diff, skipping most cache tables and other unnecessary tables:

mysqldump  -uUSER -pPASSWORD DBNAME --skip-extended-insert --complete-insert --skip-opt --ignore-table=DBNAME.cache --ignore-table=DBNAME.cache_filter --ignore-table=DBNAME.cache_menu --ignore-table=DBNAME.cache_page --ignore-table=DBNAME.cache_field --ignore-table=DBNAME.cache_update --ignore-table=DBNAME.cache_views_data --ignore-table=DBNAME.history --ignore-table=DBNAME.sessions --ignore-table=DBNAME.watchdog --ignore-table=DBNAME.cache_admin_menu --ignore-table=DBNAME.cache_block --ignore-table=DBNAME.cache_form --ignore-table=DBNAME.cache_libraries --ignore-table=DBNAME.cache_metatag --ignore-table=DBNAME.cache_views --ignore-table=DBNAME.cache_bootstrap --ignore-table=DBNAME.cache_features --ignore-table=DBNAME.cache_image --ignore-table=DBNAME.cache_panels --ignore-table=DBNAME.cache_path --ignore-table=DBNAME.cache_variable --ignore-table=DBNAME.cache_eck --ignore-table=DBNAME.cache_search_api_solr --ignore-table=DBNAME.cache_token --ignore-table=DBNAME.ctools_css_cache --ignore-table=DBNAME.ctools_object_cache > 1.sql

(repeat and save the output to 2.sql)

diff -u 1.sql 2.sql

Delete an alias for a node:

drush sql-query "delete from url_alias where source = 'node/48561'"

Delete an alias by path (e.g. /example):

drush sql-query "delete from url_alias where alias = 'example'"

Get corresponding (reverse) entity references. (That is, all entity ids that reference a specific entity inside an entityreference field)

drush sql-query "select entity_id, bundle from field_data_field_{entityreference_field_machine_name} where field_{entityreference_field_machine_name}_target_id = {entity_id}"

For example, if the field's machine name is "form_reference", and the reference is "node/141":

drush sql-query "select entity_id, bundle from field_data_field_form_reference where field_form_reference_target_id = 141"

More...

A more general and exhaustive list can be found here: https://groups.drupal.org/node/28088
See also www.drushcommands.com.