Skip to content

Instantly share code, notes, and snippets.

@ivangrynenko
Created March 10, 2026 22:58
Show Gist options
  • Select an option

  • Save ivangrynenko/58bef0edd9dfef79de2bb3291d5f39b7 to your computer and use it in GitHub Desktop.

Select an option

Save ivangrynenko/58bef0edd9dfef79de2bb3291d5f39b7 to your computer and use it in GitHub Desktop.
Examples of using drush sql:sanitize to prepare production database for developers
```bash
# Pure Drush / SQL only.
# Run from the project root.
# Shows multiple examples of using drush to sanitize a database depending on the intentions.
export DRUSH="./vendor/bin/drush --root=./web -y"
export SANITIZE_PASSWORD='ChangeMe123!'
export SANITIZE_EMAIL_PATTERN='user+%uid@example.test'
export ADMIN_EMAIL='admin@example.test'
# Change passwords, keep email addresses, preserve usernames.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email=no
# Change passwords and email addresses, preserve usernames.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email="${SANITIZE_EMAIL_PATTERN}"
# Randomize passwords, keep email addresses, preserve usernames.
$DRUSH sql:sanitize \
--sanitize-password \
--sanitize-email=no
# Change passwords and email addresses, preserve selected roles.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email="${SANITIZE_EMAIL_PATTERN}" \
--ignored-roles=administrator,site_admin
# Change passwords and email addresses, preserve selected custom user fields.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email="${SANITIZE_EMAIL_PATTERN}" \
--allowlist-fields=field_phone,field_biography
# Change passwords and email addresses, but keep webform submissions.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email="${SANITIZE_EMAIL_PATTERN}" \
--sanitize-webform-submissions=no
# Change passwords and email addresses, then replace usernames with emails.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email="${SANITIZE_EMAIL_PATTERN}"
$DRUSH sql:query "UPDATE users_field_data SET name = mail WHERE uid <> 0;"
# Change passwords, keep email addresses, then replace usernames with emails.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email=no
$DRUSH sql:query "UPDATE users_field_data SET name = mail WHERE uid <> 0;"
# Keep usernames explicitly: no extra SQL needed.
$DRUSH sql:sanitize \
--sanitize-password="${SANITIZE_PASSWORD}" \
--sanitize-email="${SANITIZE_EMAIL_PATTERN}"
# Project-specific follow-up SQL from this repo: clear uid 0 name/mail.
$DRUSH sql:query "UPDATE users_field_data SET mail = '', name = '' WHERE uid = 0;"
# Project-specific follow-up SQL from this repo: restore uid 1 email.
$DRUSH sql:query "UPDATE users_field_data SET mail = '${ADMIN_EMAIL}' WHERE uid = 1;"
# Project-specific follow-up SQL from this repo: disable uid 1.
$DRUSH sql:query "UPDATE users_field_data SET status = 0 WHERE uid = 1 AND langcode = 'en';"
# Manual webform submission cleanup only.
$DRUSH sql:query "TRUNCATE webform_submission;"
$DRUSH sql:query "TRUNCATE webform_submission_data;"
# Optional manual cleanup for webform submission log table if present.
$DRUSH sql:query "TRUNCATE webform_submission_log;"
# Optional queue / watchdog cleanup examples.
$DRUSH sql:query "TRUNCATE queue;"
$DRUSH sql:query "TRUNCATE watchdog;"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment