4

I want to remove a set of users by id from a Joomla 3.x database. I am trying to figure out from the documentation if this is possible without having to write my own SQL for each database table.

Something like the following pseudo code:

 delete from #_users where ( `lastvisitDate` < NOW() - INTERVAL 1 MONTH  ) AND ( #__user_usergroup_map.group_id = 18 );
 delete from #_user_usergroup_map where user_id not in (select id from #_users);
 delete from #_user_profiles where user_id not in (select id from #_users);

I am familiar with the querybuilder see: https://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase/en

But if there is already code in the framework that deletes user's with details then it would be my preference not to reinvent the wheel.

The documentations has this : https://api.joomla.org/cms-3/classes/Joomla.CMS.User.User.html but it seems that no parameters are passed to user->delete?

I'm assuming this can be done as it is possible to tick boxes in the administrator back end user list and delete multiple users.

I have been working with Joomla for a few years (since 1.5) but truth be told I have always struggled with the API documentation.

Any help would be greatly appreciated.

| improve this question | | | | |
  • Welcome to JSE. Please take the tour. You have a good question. May I just confirm that you desire a "hard delete" rather than a "soft delete" of users? – mickmackusa Nov 25 '19 at 3:11
  • Yes a complete removal of specific user data would be my preference. (non-recoverable) – Ryan Nov 25 '19 at 5:17
  • Actually its fairly common for users to request complete data removal from systems. and it can be achieved manually through the back end by ticking multiple users and hitting the delete button. I guess this runs $user->delete on each instance that is ticked. – Ryan Nov 25 '19 at 23:02
  • Okay, I believe you, I didn't really have a look. I'll delete my comment @Ryan – mickmackusa Nov 25 '19 at 23:11
  • 1
    Ha yea I'm in Bayside currently chained to my desk, stuck in the matrix. :) I'll let you know when Morpheus arrives with my pills. – Ryan Dec 5 '19 at 0:00
3

The documentations has this : https://api.joomla.org/cms-3/classes/Joomla.CMS.User.User.html but it seems that no parameters are passed to user->delete?

That's correct. This method works on the user instance.

use Joomla\CMS\User\User;

// Get user with ID 1.
$userId = 1;
$user   = User::getInstance($userId);

// Delete the user.
if (!$user->delete())
{
    // Error occurred.
}
else
{
    // User with ID 1 deleted.
}

Using this method triggers onUserBeforeDelete and onUserAfterDelete events so plugins can delete related user data.

| improve this answer | | | | |
  • This may be what I want. Would this remove usergroup mapping data and profile data if I put it in a foreach loop with an array of ID's – Ryan Nov 25 '19 at 23:04
  • 1
    User group mapping data will be removed. Assuming you mean profile data added by User - Profile plugin, it will be deleted provided the plugin is enabled. – Sharky Nov 26 '19 at 6:21
  • 1
    I agree totally with Ryan re state of Joomla API documentation and I'm currently trying to pull together a set of good API guides like docs.joomla.org/Accessing_the_current_user_object, so contributions very welcome :-) ! I've just updated it to take account of Sharky's comments re related data being deleted, but just to note that if the user has created articles or is set up as a linked contact then the user's id in those records remains. But it's just the id, so no personal data afaik. – Robbie Jackson Dec 18 '19 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.