How to fix code registry in Drupal 7

10 Mar

Almost all Drupal developers have encontered this issue when they were trying to move a module from one enviroment to another or when they copied the module folder into a wrong directory. 

What is code registry in Drupal 7?

Drupal 7’s code registry is an inventory of all classes and interfaces for all enabled modules and Drupal's core files. The registry stores the path to the file that a given class or interface is defined in, and loads the file when necessary.

How can I notice if my registry is broken? 

Well, that's easy. You can get an error that looks like: PHP Fatal error: Class 'EntityAPIControllerExportable' not found in ...sites/all/modules/rules/includes/rules.core.inc on line 11

How can I fix my broken registry?

Luckily, fixing the registry is quite easy with Registry Rebuild, which is not a Drupal module but a script which rebuilds all your website registries.

First of all, making a database backup is recommended because your registry table will be overwritten. For the backup you can use drush sql-dump >dump.sql or mysqldump -uuser -ppassword dbname >dump.sql or use phpMyAdmin.

From your console download via drush the registry rebuild project drush dl registry_rebuild or just unzip the project into sites/all/modules/.

After that run drush registry-rebuild, drush rr or cd sites/all/modules/registry_rebuild php registry_rebuild.php. If you don`t use a terminal just call http://example.com/sites/all/modules/registry_rebuild/registry_rebuild.php into your browser.

If you get a message that looks like the one below then your registry has been rebuilt.

DRUPAL_ROOT is /var/www/example.
Bootstrap caches have been cleared in DRUPAL_BOOTSTRAP_SESSION
Doing registry_rebuild() in DRUPAL_BOOTSTRAP_SESSION
Rebuilding registry via registry_rebuild_cc_all in DRUPAL_BOOTSTRAP_FULL
All caches have been cleared with registry_rebuild_cc_all.
Deleted 1 stale files from registry manually.There were 1111 files in the registry before and 1110 files now.
All caches have been cleared with registry_rebuild_cc_all.
If you don't see any crazy fatal errors, your registry has been rebuilt.

Now go check your website. Enjoy!  
Leave A Comment