Did you try running the synchronization script after you emptied the external database?
To answer your question:
How does moodle update the enrollment from the external database?
Every time a user logs in, it checks their enrollments against the external database and updates it accordingly.
From the documentation:
A script is provided that can synchronize all your user enrollments at once - both adding and removing user enrolments (and creating courses if specified). The script is called sync.php and is found in the enrol/database/cli folder.
This script is meant to be called from a system cronjob to sync moodle enrolments with enrolments in the external database. You need to make sure all the users present in the external enrolments are already created in moodle. If you are using external authentication plugins (db, ldap, etc.) you can use the scripts provided by those plugins to synchronize your users before running this script.
Example cron entry for Moodle 2.5
# 5 minutes past 4am
5 4 * * * /usr/bin/php -c /path/to/php.ini /path/to/moodle/enrol/database/cli/sync.php
- If you have a large number of enrolments, you may want to raise the memory limits by passing -d memory_limit=256M
- For debugging & better logging, you are encouraged to use in the command line: -d log_errors=1 -d error_reporting=E_ALL -d display_errors=0 -d html_errors=0
- This only works for users that already exist in your Moodle site (see comment above)