# Is there a way to bulk-change user 'names' (note: not usernames)

**URL:** https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315
**Category:** Support
**Created:** [October 31, 2019, 11:55am UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315 "2019-10-31T11:55:57Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![pjv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pjv/32/115994_2.png) [@pjv](https://meta.discourse.org/u/pjv)
#### Post date: [October 31, 2019, 11:55am UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/1 "2019-10-31T11:55:57Z")

</div>

We have a few hundred users on our forum whose names (not usernames, _names_) inadvertently got set to their email address as a result of how the external SSO provider (through wordpress) was set up when they created their accounts.

Is there a relatively simple way to bulk change those names from `the_name@example.com` to `the_name` (i.e. truncate everything from ‘@’ onward) without manually going into each profile and changing them one by one?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 31, 2019, 12:43pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/2 "2019-10-31T12:43:39Z")

</div>

You can do it from the rails console. It’s more complicated than can be explained here, especially since emails addresses are no longer in the user record. If you have a budget you can post in #Marketplace or contact me directly.

Or but you don’t care about the actual email address…

Something like

```plaintext
users = User.where ('username like'%@%''")

```

Then you’d loop through and use `gsub` to strip everything from the @ to the end. That’s the best I can do on my phone.

---

<div class="post-metadata">

### Author: ![pjv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pjv/32/115994_2.png) [@pjv](https://meta.discourse.org/u/pjv)
#### Post date: [October 31, 2019, 3:34pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/3 "2019-10-31T15:34:43Z")

</div>

I appreciate the response @pfaffman. I can write the SQL, but working in a rails console is a little outside my wheelhouse. How can I get into the discourse python console 🙂?

Would be nice - though I understand also potentially dangerous - if there was a tool like the [data explorer](https://meta.discourse.org/t/32566?silent=true) that also let you write an UPDATE query.

Anyone know if there is a way to connect a third party SQL tool (like Sequel Pro or equiv) to the running DB in self-hosted docker discourse?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 31, 2019, 4:04pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/4 "2019-10-31T16:04:48Z")

</div>

> [@pjv](#):
>
> I can write the SQL, but working in a rails console is a little outside my wheelhouse.

Yeah, but doing it in Rails makes it much harder to screw up your database, like if you do it with Rails, you’ll know that you didn’t make usernames that are, say, illegal (and if some other table needs to be updated when you change the one your messing with, Rails will do it, SQL won’t).

```plaintext
cd /var/discourse
./launcher enter app
rails c

```

Now you’re in rails.

```plaintext
users=User.where("username like '%@%'")
users.each do |user|
  user.name.gsub!(/@(.*)/,"")
  puts "New name: #{user.name}"
  # user.save
end

```

I think that’ll do what you want. If it seems like it, you can uncomment the `user.save` and try your luck.

Do a backup first, OK? (Or, better, on a staging server.)

---

<div class="post-metadata">

### Author: ![pjv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pjv/32/115994_2.png) [@pjv](https://meta.discourse.org/u/pjv)
#### Post date: [October 31, 2019, 4:11pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/5 "2019-10-31T16:11:40Z")

</div>

> [@pfaffman](#):
>
> Do a backup first, OK? (Or, better, on a staging server.)

Wow, thanks for the code!

I don’t have a staging server for this discourse instance, but i can take a backup and then run the code very early tomorrow morning when there is likely not much (or any) traffic so i can restore immediately if there’s an issue.

Thanks so much @pfaffman.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 31, 2019, 4:32pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/6 "2019-10-31T16:32:42Z")

</div>

You should be pretty safe if the `puts` look ok. Cheers!

---

<div class="post-metadata">

### Author: ![lionel-rowe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lionel-rowe/32/134288_2.png) [@lionel-rowe](https://meta.discourse.org/u/lionel-rowe)
#### Post date: [October 31, 2019, 5:50pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/7 "2019-10-31T17:50:56Z")

</div>

Truncating at the `@` still leaves personal information (email address) largely visible to the Big Bad Internet (given that most people are `@gmail.com` or a few other big providers). You’d probably be better off setting them to the same value as username.

---

<div class="post-metadata">

### Author: ![pjv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pjv/32/115994_2.png) [@pjv](https://meta.discourse.org/u/pjv)
#### Post date: [October 31, 2019, 5:54pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/8 "2019-10-31T17:54:12Z")

</div>

Thanks @lionel-rowe, that’s a good point. The usernames already are that same value though (username was created by taking everything prior to ‘@’ from the user’s email address).

---

<div class="post-metadata">

### Author: ![pjv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pjv/32/115994_2.png) [@pjv](https://meta.discourse.org/u/pjv)
#### Post date: [November 1, 2019, 12:04pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/9 "2019-11-01T12:04:05Z")

</div>

> [@pfaffman](#):
>
> Now you’re in rails.

The horror.

I had to make a minor change in the code and add a little procedure to make it work. Here’s the modified code:

`users=User.where("name like '%@%'")`

Then the rails console starts “more-ing” you through the user data so i needed to `q` out of that. Then:

```plaintext
users.each do |user|
  user.name.gsub!(/@(.*)/,"")
  puts "New name: #{user.name}"
  # user.save
end

```

Then the rails console showed me the list of new names (correct!), and then put me back into a “more” of the users. `q` again.

Then:

```plaintext
users.each do |user|
  user.name.gsub!(/@(.*)/,"")
  puts "New name: #{user.name}"
  user.save
end

```

…and no more email address user names and everything looking good on the forum front end.

Thanks again @pfaffman for your generous help.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 18, 2023, 8:04pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/10 "2023-07-18T20:04:43Z")

</div>



---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [July 18, 2023, 8:21pm UTC](https://meta.discourse.org/t/is-there-a-way-to-bulk-change-user-names-note-not-usernames/132315/11 "2023-07-18T20:21:48Z")

</div>


