How to delete users completely from Moodle

Founded in Stakoverflow and owner of this solution is Sahil Purav

Moodle has some weird behaviour in terms of cleaning up site. e.g. If we are deleting users from admin account, the entry still remains in database table {user}. Most probably it is because that deleted user may have other historical posts. But there is no way to clean up deleted user from database.

Here is an approach that may actually work (I didn’t test it though).

  1. Logged in with SSH
  2. Executed following command from putty: php /path/to/moodle/admin/cli/fix_deleted_users.php
  3. Then went in Database and put following query: DELETE FROM mdl_user WHERE deleted = 1

Everything was working properly. Basically, fix_deleted_users.php removes all references of deleted user and then leaves those empty deleted users in mdl_user table which can be further deleted. We only need to take care of third party plugins that may use deleted user’s ID but in my case, I did not installed any plugin so it went successful.

Surprisingly, I did not find any documentation on this file on internet. Hope it helps someone.

Copied from: http://stackoverflow.com/questions/33547376/moodle-clean-up-database