Yet another web developer blog

How to view and revert installed schema version of module in Drupal 8, 9

After executing hook_update_N functions, Drupal stores last version of installed updates in his key-value storage, if update function executed without exceptions.

You can simply lookup installed version via command-line drush command:

drush ev "var_dump(drupal_get_installed_schema_version('my_module'))"

Sometimes you may need revert version to previous, for re-apply updates, or repeat updates, if they previously executed with problems.

You can set previous version of Drupal module schema version via this cli drush command:

drush ev "drupal_set_installed_schema_version('my_module', 8005)"

and re-run updates via:

drush updb

In PHP code you can use those functions for get and set installed schema versions:

// Get version using Drupal function:
$version = drupal_get_installed_schema_version('my_module');
// or get version directly from key-value storage:
$version = \Drupal::keyValue('system.schema')->get($module);  

// Set version using Drupal function:
drupal_set_installed_schema_version('my_module', 8005);
// or set version directly to key-value storage:
\Drupal::keyValue('system.schema')->set($module, 8005);