Getting email job failures en masse - due to missing user?

I’m getting a lot of email job failures, when I check the logs in Sidekiq, I can see that they’re related to a user with ID 2. The failures are on this form:

Jobs::HandledExceptionWrapper: Wrapped NoMethodError: undefined method `email' for nil:NilClass

I think the user with ID 2 was created by accident through logging on with SSO using the wrong email address, and I since cleaned it out. I guess that user ID is somehow still lingering in parts of the system? How can I verify that these issues are indeed relating to a non-existent user, and if so get rid of it?

How did you ‘clean it out’? Using the rails console?

Yeah I used the rails console to remove the user. I think I removed the main database record. I found there was an SSO record lingering after though, so I reset the SSO records too somehow.

Any idea how to troubleshoot what’s going wrong here? As I said, I don’t think there’s a user record with ID 2 any longer. Unless another user was added with that ID in the meantime, there seems to be remnants in the system causing these email errors.

Deleting a user is a complicated process, and by doing it on the console you have bypassed all the safeguards in place. If you can restore a backup and do it from the user interface, that would be best.

If not, maybe you could try creating a new user object on the console using the same ID as the old user, and then deleting that user properly. I would strongly recommend taking a backup before attempting that, as I’m not sure exactly what will happen.


I don’t have a backup from before doing so unfortunately.

How do I create a new user and delete it (properly)?

Take a backup first, this could cause issues

I’ve never done this, but try running:

User.create!(id: 2, password: SecureRandom.hex, email: "", username: 'someusername')

Then that user should appear in the user interface, and you can try deleting it from the admin panel.

Thanks! I tried creating the user now, as you said, but it turns out the record with ID 2 is still there, even though I thought I had deleted it! It doesn’t show up among the users in the Discourse UI though. Is there anything I can do further to resolve this issue?

Does it appear at /admin/users/39129/username?

That gives me a 404 :confused:

Oops, I meant to change the number there


Also sorry for editing your post, I hit the wrong button!

1 Like

Ah, that does give me a page of the user!

Shall I click Delete User on that page?

1 Like

Give it a try. That should then run through the ‘proper’ deletion process

There was an error deleting that user. Make sure all posts are deleted before trying to delete the user

Check /logs to see a more detailed error (hopefully)

I don’t see anything beneath $SITE/logs, but in /shared/log/rails/production.log I can see the following:

Processing by Admin::UsersController#destroy as JSON
  Parameters: {"context"=>"/admin/users/2/username", "delete_posts"=>"true", "id"=>"2"}
  Rendering text template
  Rendered text template (0.0ms)
Completed 418  in 11ms (Views: 3.2ms | ActiveRecord: 3.4ms)
Failed to process hijacked response correctly : undefined method `email' for nil:NilClass
Started GET "/admin/users/2.json?_=1547652146066" for at 2019-01-16 15:22:31 +0000
Processing by Admin::UsersController#show as JSON
  Parameters: {"_"=>"1547652146066", "id"=>"2"}
Completed 200 OK in 64ms (Views: 0.2ms | ActiveRecord: 42.7ms)```

Try running


on the console. I guess it will say nil?

[2] pry(main)> User.find(2).primary_email
=> nil

Ok, it looks like you deleted the UserEmail record rather than the user. So try

u = User.find(2) = ""!

Then try deleting from the UI again