XF 2.2 How to properly delete users via PHP?

Mika_M

Member
I'm trying to make a small addon to automatically delete users which have not visited in last 2 years.

PHP:
$finder = \XF::finder('XF:User');
$users = $finder->where('last_activity', '<=', time() - 2 * 31556926)->fetch();

After retrieving users, what would be the right and proper way to delete them? No renaming needed, only delete users like the batch update via ACP would do it.
 
PHP:
foreach ( $users as $user ) $user->delete();

delete() accepts 2 parameters:
$throw: defaults to true, change to false if you want the script to continue the loop despite errors.
$newTransaction: defaults to true, change to false if you want the script to roll back all db transactions if an error occurs.
 
Last edited:
Something is still missing perhaps? This does work, user(s) get deleted, but it throws server error:

ErrorException: Job XF:SearchUserChange: [E_DEPRECATED] strlen(): Passing null to parameter #1 ($string) of type string is deprecated src\XF\Search\Source\MySqlFt.php:217
 
This error (as you can see in its description) hasn't to do with pure user deletion, there's a search job running. It could be related to a completely different part of your script or it could be an add-on injecting code into the deletion process in a wrong way. You'll need to have someone look at your code base.
 
Top Bottom