I have thousands of spam users. I use K2 and I would want to delete them from "k2_users" and from "users". I want to delete the blocked ones. I have tried deleting K2_users from the admin, but it does not delete the user from "users".

Any ideas to delete from both tables(using SQL) the users that are blocked? I think that the field "block" of the table "users" is the key.


  • I am sure that I can help you to write a couple of delete queries to purge the spammers from your two tables. I don't have K2 on my set up, nor am I running Joomla2.5. If you can export your two tables and a few relevant rows of sample data I'll be able to offer you support. meta.stackoverflow.com/a/333953/2943403 Nov 16, 2018 at 21:04

1 Answer 1


Deleting users using Joomla's User Manager is also going to delete them from K2 users table, as long as User - K2 plugin is enabled.

To delete users programmatically, you can query the database to get the IDs of blocked users and pass them to UsersModelUser::delete().

$db = JFactory::getDbo();
$query = $db->getQuery(true)
    ->where($db->quoteName('block') . ' = 1');

if ($blockedUsers = $db->setQuery($query)->loadColumn())
    JLoader::register('UsersModelUser', JPATH_ADMINISTRATOR . '/components/com_users/models/user.php');

    $model = JModelLegacy::getInstance('User', 'UsersModel', array('ignore_request' => true));
  • Ok, and do you know which is the SQL in order to delete it massively? There are too many records...
    – Za7pi
    Nov 19, 2018 at 7:08
  • 1
    Because deleting users triggers a plugin event, you should not use SQL queries to delete them. Doing so prevents the event from firing and so plugins that act on user deletion will not be triggered. Thus some references of the user may remain in other tables.
    – Sharky
    Nov 19, 2018 at 7:25
  • Updated the answer with a way to delete blocked users using the model.
    – Sharky
    Nov 19, 2018 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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