Removing Old Users

Removing Old Users

by Oliver Webb -
Number of replies: 2

Hi,

Is there a function in Moodle to completely remove old user accounts based on last access?

We have had our Moodle installation for a number of years, and it contains a lot of pupil accounts that have now left the school. For example, if I sort users by last access, there are lot's who last accessed Moodle 5/6 years ago.

What's the best way to remove these accounts?

Thanks,

Ollie

Moodle 3.1.4+ (Build: 20170209)

Average of ratings: -
In reply to Oliver Webb

Re: Removing Old Users

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

A cautious yes, see Bulk_user_actions

You can select users based on their last active date and then delete all the selected ones. 

Make sure you get the before/after right wink

Average of ratings: Useful (1)
In reply to Oliver Webb

Re: Removing Old Users

by Mahinwal Surya -

Hi, 

I think this will help you.

include these files on top of your page.

require_once('../../config.php');

require_once($CFG->dirroot."/lib/moodlelib.php");

global $CFG, $DB, $USER;

and

call this function: delete_user(stdClass $user);

NOTE:

  •  Marks user deleted in internal user database and notifies the auth plugin.
  •   Also unenrols user from all roles and does other cleanup.
  •   Any plugin that needs to purge user data should register the 'user_deleted' event.
  •  @param stdClass $user full user object before delete
  •  @return boolean success
  •  @throws coding_exception if invalid $user parameter detected.


Thanks