Drush Commands


See main 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.

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'"

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('pere@example.com', 'Subject', 'Message', 'From: pere@example.com');"

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 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 host name. 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();"

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. Seehttp://drupal.stackexchange.com/q/17657/10086
If does not work because Drupal cannot bootstrap, do it that way:
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

Change error level
Show only errors and warnings
drush vset -y 1
Show all
drush vset -y 2
Hide everything
drush vset -y 0

Enable maintenance mode
drush vset -y maintenance_mode 1

Open a MySQL console logged in
drush sql-cli

Import a backup of the database
drush sql-cli < dump.sql

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

Delete all content of specific content types
sudo drush generate-content 0 --kill --types=facebook,instagram,tweet
Note that you need Devel and Devel Generate modules. Alternatives are drush migrate-wipe and Delete all module.

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

Generate random content
sudo drush generate-content 20 --types=page

Create a boilerplate for a new module
drush mb my_module menu cron --write --name="My module"

Run sanitization operations on the current, non-production database
drush sql-sanitize
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

Comments

  1. Get configurations with or without overrides:
    drush config-get my.config
    drush config-get my.config --include-overridden

    ReplyDelete
  2. #Install Drush Globally
    - Go to https://github.com/drush-ops/drush-launcher
    - Find steps and Install Phar file
    - chmod +x drush.phar
    - sudo mv drush.phar /usr/local/bin/drush
    - check if installed correctly drush -v
    - To update use drush self-update

    ReplyDelete
  3. // List changes before exporting
    drush config:export

    // List with git diff before exporting config
    drush config:export --diff

    ReplyDelete
  4. To delete all entities of given type
    drush entity:delete shortcut_set

    ReplyDelete

Post a Comment

Drupal Contribution
Git Commands
RESTful Services
Lando Commands
Docker Commands
MySQL
Database Quick Code
Drush Commands
Drupal Console
PHP Quick Code
Drupal Quick Code
Composer Commands
Linux Commands
Linux Shell Scripting
Drupal Hooks
Twig Tricks
PHPUnit Test
PhpMyAdmin
Drupal Constants
CSS Clues
BLT Commands
Vagrant Commands
Localhost
127.0.0.1
Drupal Interview
Drupal Certifications
Concept & Definitions
Mac Tips
Windows Tips
Browser Tips

Best Practice

Use 'elseif' instead of 'else if'
#CodingTips

As of PHP 5.4 you can also use the short array syntax, which replaces array() with []
#CodingTips

Functions in general shall be named using snake_case(say, my_function()), and using camelCase(say, myFunction()) when declared within a plugin class
#CodingTips

Variables in general shall be named using snake_case(say, $my_variable), and using camelCase(say, $myVariable) when declared within a plugin class
#CodingTips

Manage automatically assigning of new permissions whenever a module is enabled here- admin/config/people/accounts
#ConfigurationTips

Manage source of Main-menu and User-menu links here- admin/structure/menu/settings
#ConfigurationTips

Helper function(s) shall be named prefixing an underscore(say, _my_helper_function()), which can prevent hooks from being called
#CodingTips

Ideally, configuring of 'Private file system path' at admin/config/media/file-system should be located outside of your Drupal root folder(say, ../my_private_files)
#ConfigurationTips

You should be aware that uploading files as 'Private file' will slow down the process of loading the files as Drupal has to be bootstrapped for every file that needs to be downloaded
#ConfigurationTips #BeAware

Code should always be pushed up(dev -> staging -> production) and databases should only be pushed down(production -> staging -> dev)
#DevelopmentTips

Get Raw SQL Query of drupal dynamic queries before executing it using $query->__toString();
#DebugTips

In VI-Editor, Press ESC key to come in command mode and for undo type :U and for redo type :Ctrl+R
#LinuxTips

Insert queries must always use a query builder object(layer of abstraction), allowing individual database drivers special handling for column values (if applicable), example case for LOB and BLOB fields.
#DatabaseQueryTips

Drupal uses the .inc extension to prevent files from being executed directly.
#DevelopmentTips

Popular Posts