Skip to content

Instantly share code, notes, and snippets.

@xurizaemon
Last active September 23, 2020 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xurizaemon/b29dd1fb71b7048efffc018bc8ff1262 to your computer and use it in GitHub Desktop.
Save xurizaemon/b29dd1fb71b7048efffc018bc8ff1262 to your computer and use it in GitHub Desktop.
Dropping Drupal Migrate Tables

Dropping Migrate tables

Please test these thoroughly and/or export the list of tables to drop to a list then write a tidy update hook rather than blowing away data you didn't mean to.

It's a safer idea to write an update hook or export a list of tables to target because it may not be a fair assumption that every table named migrate_* is one you can delete without impact. That's why the core functionality proposed uses a scan for known migrations rather than just deleting tables based on a pattern match.

That said, here are some quick solutions:

Drupal Update hook

/**
 * Clean up Migrate tables
 */
function example_update_8001() {
  $database = \Drupal::database();
  if ($migrate_tables = $database->schema()->findTables('migrate_%')) {
    foreach ($migrate_tables as $table) {
      $database->schema()->dropTable($table);
    }
  }
}

Or, from the CLI

Postgres

drush sqlq '\dt migrate_*' | awk '{ print $2 }' | while read TABLE ; do
  echo "Dropping $TABLE" && drush sqlq "DROP TABLE $TABLE" 
done

MySQL

drush sqlq "show tables like 'migrate_%'" | while read TABLE ; do
  echo "Dropping $TABLE" && drush sqlq "DROP TABLE $TABLE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment