How can I programmatically deactivate multiple user accounts?
The problem is that you load the users two times. In the foreach loop you already have the loaded entity available in $user.

Change your foreach loop to look like this and it should work:

foreach ($users as $user) {
  $user-&gt;set("status", 0);
  $user-&gt;save();
}


You can also remove the $storageHandler since you don't use that any more.