# Mass-edit email of all users

**URL:** https://meta.discourse.org/t/mass-edit-email-of-all-users/51687
**Category:** Support
**Created:** [10월 18, 2016, 11:17오전 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687 "2016-10-18T11:17:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Raku](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@Raku](https://meta.discourse.org/u/Raku)
#### Post date: [10월 18, 2016, 11:17오전 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/1 "2016-10-18T11:17:22Z")

</div>

Long story short: I screwed up an import from phpBB. :facepalm: Well okay, that’s not the full story.

The import itself worked flawlessly!  
And at this point let me shout out to the whole team for creating an awesome piece of software (from a users point of view, an admins point of view and a devs point of view)! 🙂

But…  
… I wanted to be on the safe side and edited every users email in the sql file from phpBB. So Discourse won’t send out any mails after the import, because we needed some time to restructrue posts and categories after the import. Please, don’t ask why and how. 😂  
After we finished all refurbishing tasks we wanted to edit the users emails back to their original one, only to find out admins can’t edit emails. We ended up impersonating every user and changing their email address back… only to find out the activation link may only be valid for a few (?) hours.  
At least we now have users who click the link they received, but Discourse doesn’t change their address.

Luckily I didn’t screw up THAT bad. When editing the users mail in the first place, before the import, I just appended kinda nonce to the @-sign, so the domain is invalid. This means the original data is still there.

My plan is to replace the nonce with an empty string on the database. And that’s where I need help, as I have zero experience with Ruby/Rails.

I already found

> [@Edit a user preference for everyone or a subset of users](https://meta.discourse.org/t/edit-a-user-setting-for-all-discourse-users/25162):
>
> bookmark This guide explains how to edit a user preference for everyone or a subset of users in Discourse. person_raising_hand Required user level: System Administrator warning Console access is required. If you need to update the user preference for all of your users or a large subset of users, you can do so via the rails console. Summary In this guide, you’ll learn: How to access the Rails console for making bulk changes Examples of modifying user preferences How to identify se…

And that’s quite a good start, but I need further help here.

What I think I need to to - in _pseudocode_:

```
./launcher enter app
rails c
User.where("email contains nonce").update_all(replace nonce with empty string)

```

Or even

```
User.update_all(replace nonce with empty string in email field)

```

As the nonce is really just some gibberish behind the @-sign, but the same gibberish for every user. And the ones I already corrected manually won’t be affected by this.

I don’t have a clue what I’m supposed to write in between the parantheses. Or if this (a replace) is even possible.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [10월 18, 2016, 1:40오후 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/2 "2016-10-18T13:40:32Z")

</div>

```ruby
User.update_all("email = replace(email, 'nonce', '')")

```

---

<div class="post-metadata">

### Author: ![Raku](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@Raku](https://meta.discourse.org/u/Raku)
#### Post date: [10월 18, 2016, 4:40오후 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/3 "2016-10-18T16:40:08Z")

</div>

Worked like a charme! Thank you, Falco! 🙂

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [10월 18, 2016, 11:59오후 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/4 "2016-10-18T23:59:07Z")

</div>

> [@Raku](#):
>
> only to find out admins can’t edit emails

Yes, they can-- edit it like a user would, from the profile settings.

---

<div class="post-metadata">

### Author: ![Raku](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@Raku](https://meta.discourse.org/u/Raku)
#### Post date: [10월 19, 2016, 7:34오전 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/5 "2016-10-19T07:34:05Z")

</div>

[quote=“codinghorror, post:4, topic:51687, full:true”]edit it like a user would, from the profile settings.  
[/quote]

Oh my… it seems we really overlooked this! :badpokerface:  
We impersonated each and every user and changed it there.

Will Discourse send out a mail asking for verification of the adress, if an admin changes it in the profile settings (without impersonating the user)?

---

<div class="post-metadata">

### Author: ![omarfilip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/omarfilip/32/208019_2.png) [@omarfilip](https://meta.discourse.org/u/omarfilip)
#### Post date: [10월 21, 2017, 6:52오후 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/6 "2017-10-21T18:52:09Z")

</div>

> [@Falco](#):
>
> User.update\_all(“email = replace(email, ‘nonce’, ‘’)”)

I’m trying something similar, but getting a readonly error. How do I get around it? This is not a production instance, so it’s OK if I screw up.

```
User.update_all("email = replace(email, '@', '@invalid_email')")
ActiveRecord::StatementInvalid: PG::RaiseException: ERROR: Discourse: email in users is readonly
: UPDATE "users" SET email = replace(email, '@', '@invalid_email')
from /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rack-mini-profiler-0.10.5/lib/patches/db/pg.rb:90:in `async_exec'

```

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [10월 21, 2017, 7:57오후 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/7 "2017-10-21T19:57:24Z")

</div>

Emails have moved to a separate table called `user_emails` (behind the scenes, users now can have multiple email addresses), so this command will have to be rewritten.

This may work:

```ruby
UserEmail.update_all("email = replace(email, 'nonce', '')")

```

…or it might set fire to the database. 🔥  
You’ve been warned 🙂

---

<div class="post-metadata">

### Author: ![omarfilip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/omarfilip/32/208019_2.png) [@omarfilip](https://meta.discourse.org/u/omarfilip)
#### Post date: [10월 21, 2017, 8:08오후 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/8 "2017-10-21T20:08:40Z")

</div>

No fire! 🤪 Thanks Felix!

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [6월 8, 2024, 12:44오후 UTC](https://meta.discourse.org/t/mass-edit-email-of-all-users/51687/9 "2024-06-08T12:44:07Z")

</div>

이 주제는 2790일 후 자동으로 닫혔습니다. 더 이상 새 답글을 작성할 수 없습니다.
