# Rake task for merging users

**URL:** https://meta.discourse.org/t/rake-task-for-merging-users/89086
**Category:** Announcements
**Tags:** new-feature, merge-user
**Created:** [March 1, 2018, 12:56pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086 "2018-03-01T12:56:29Z")
**Posts on this page:** 20
**Page:** 3

<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: [February 14, 2019, 1:36am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/41 "2019-02-14T01:36:55Z")

</div>

If you’re using Wordpress as the authority for SSO what benefit is there to storing a second email in Discourse?

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [February 14, 2019, 2:24am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/42 "2019-02-14T02:24:00Z")

</div>

right-o. unless I store it in wordpress too for the same reasons. but I don’t know how to prevent this rake task from storing a secondary email when merging users.

---

<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: [February 14, 2019, 2:26am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/43 "2019-02-14T02:26:01Z")

</div>

If secondary emails are an issue can’t they just be removed?

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [February 14, 2019, 5:37pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/44 "2019-02-14T17:37:02Z")

</div>

Yes, that is one easy solution. Is there a rake task someone can share to list all the users with secondary emails, and to delete their secondary emails?

That said, I’d prefer to see discourse evolve to take advantage of a cool new feature like secondary emails rather than have to find ways to work around it. Other people are likely to bump up against this issue too at some point and wonder why the email address is not updating in discourse when updated in wordpress.

---

<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: [February 14, 2019, 6:23pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/45 "2019-02-14T18:23:28Z")

</div>

> [@tobiaseigen](#):
>
> Is there a rake task someone can share to list all the users with secondary emails, and to delete their secondary emails?

I don’t think there’s a rake task, but you could do it from the rails console. There should be some example queries about secondary emails for [data explorer](https://meta.discourse.org/t/32566?silent=true) and some sample `.update_all` code for other rails examples. It’ll take more time than I can spare here today. If you’re stuck and want to pay, my contact info is in my profile.

---

<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: [February 15, 2019, 7:30pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/46 "2019-02-15T19:30:33Z")

</div>

See [Additional email address per user account support - #34 by zogstrip](https://meta.discourse.org/t/additional-email-address-per-user-account-support/59847/34)

> [@tobiaseigen](#):
>
> Is there a rake task someone can share to list all the users with secondary emails, and to delete their secondary emails?

Here’s what you’re looking for: [Additional email address per user account support - #34 by zogstrip](https://meta.discourse.org/t/additional-email-address-per-user-account-support/59847/34)

---

<div class="post-metadata">

### Author: ![Dannii](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dannii/32/129087_2.png) [@Dannii](https://meta.discourse.org/u/Dannii)
#### Post date: [April 13, 2019, 7:30am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/47 "2019-04-13T07:30:10Z")

</div>

I’ve written a plugin which makes it easier to run this task:

> [@Merge Users Plugin](https://meta.discourse.org/t/merge-users-plugin/114917):
>
> warning As of Discourse 2.5.0 beta 3 Merge users is built into Discourse warningSee: [Discourse 2.5.0.beta3 Release Notes](https://meta.discourse.org/t/discourse-2-5-0-beta3-release-notes/148766) This plugin will not be supported long term and may break soon.. Update Dec 2022: I think a recent software update has meant that the plugin causes an error when someone is not an admin: Unhandled Promise Rejection: Error: Could not find module admin/models/admin-user imported from discourse/plugins/discourse-merge-users/discourse/components/merge-users-form So if …

---

<div class="post-metadata">

### Author: ![wesochuck](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wesochuck/32/108706_2.png) [@wesochuck](https://meta.discourse.org/u/wesochuck)
#### Post date: [December 18, 2019, 5:34pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/48 "2019-12-18T17:34:40Z")

</div>

So, if you’re using SSO on your site is it not possible to complete the rake merge task? The fine folks over at [Communiteq](https://www.communiteq.com) (formerly DiscourseHosting) say they get this error message:

```
ActiveRecord::RecordNotSaved: Failed to save the new associated primary_email
/var/www/discourse/app/services/user_merger.rb:359:in `delete_source_user'

```

This also causes issues when trying to use the plugin from @Dannii because it looks like his process is trying to do this step first.

Any suggested workarounds for merging users for SSO sites? What we have happen is that a person changes from one organization to another and so they get a new account through the SSO process (they have a new email), but since it is the same person, we’d like to be able to merge the accounts once they’ve established themselves at their new location.

---

<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: [December 19, 2019, 3:18am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/49 "2019-12-19T03:18:14Z")

</div>

Disable SSO, merge, then re-enable SSO?

---

<div class="post-metadata">

### Author: ![wesochuck](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wesochuck/32/108706_2.png) [@wesochuck](https://meta.discourse.org/u/wesochuck)
#### Post date: [December 19, 2019, 4:06am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/50 "2019-12-19T04:06:14Z")

</div>

Thanks, I gave it a shot (disable/merge/enable) but no luck. Maybe after disabling a restart is required? Which I’m assuming would mean I couldn’t login any longer since I’m an SSO user too.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [December 19, 2019, 4:15am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/51 "2019-12-19T04:15:28Z")

</div>

> [@wesochuck](#):
>
> So, if you’re using SSO on your site is it not possible to complete the rake merge task?

I don’t get this. I use sso using the wp\_discourse Wordpress plugin and have no problem running this rake task. I then delete the Wordpress user that got merged.

---

<div class="post-metadata">

### Author: ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)
#### Post date: [June 1, 2020, 2:46pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/52 "2020-06-01T14:46:15Z")

</div>

I have a specific case I need some help with, [similar one mentioned above](https://meta.discourse.org/t/rake-task-for-merging-users/89086/20).

I want to merge two user accounts, but:

- Account #1 has the old username that the user would like to keep.
- Account #2 has the user’s new email. (The user does not have access to the email in Account #1, because it’s an institutional account.)

Can I use the “Merge” UI to combine the two accounts so that the resulting account has the old username but the new email?

---

<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: [June 1, 2020, 3:58pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/53 "2020-06-01T15:58:04Z")

</div>

I don’t think that’s doable through the Admin, but it can be accomplished with an additional step:

Merge via Admin  
Change email for that user via the rails console:

```
cd /var/discourse
./launcher enter app
rails c
UserEmail.find_by(email: "old@example.com").update(email: "new@example.com")

```

---

<div class="post-metadata">

### Author: ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)
#### Post date: [June 2, 2020, 3:08pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/54 "2020-06-02T15:08:40Z")

</div>

During the merge the new email was added a Secondary Email. I wasn’t quite sure how this might interface with my custom SSO solution. So I removed the secondary email first (following [these steps](https://meta.discourse.org/t/additional-email-address-per-user-account-support/59847/53)), and then followed your suggestion.

Thanks @omarfilip!

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 2, 2020, 4:34pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/55 "2020-06-02T16:34:58Z")

</div>

> [@alehandrof](#):
>
> During the merge the new email was added a Secondary Email. I wasn’t quite sure how this might interface with my custom SSO solution.

I have run into this situation quite a few times. When users are merged, the source user’s primary email address becomes the target user’s secondary email address. When SSO is being used and the `sso overrides email` setting is enabled, the merged user may be unable to login unless the secondary email is deleted from the Rails console.

I’m wondering if the task should have an option to _not_ set the target secondary email from the source primary email.

---

<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: [June 2, 2020, 5:19pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/56 "2020-06-02T17:19:47Z")

</div>

> [@simon](#):
>
> I’m wondering if the task should have an option to _not_ set the target secondary email from the source primary email.

Or to have a convenient means to swap them.

---

<div class="post-metadata">

### Author: ![TheBaby5](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thebaby5/32/178897_2.png) [@TheBaby5](https://meta.discourse.org/u/TheBaby5)
#### Post date: [August 20, 2020, 4:52am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/57 "2020-08-20T04:52:01Z")

</div>

> [@gerhard](#):
>
> Users can be merged by executing
> 
> ```plaintext
> rake users:merge['source_username','target_username']
> 
> ```
> 
> (note: ensure there is no space before/after the comma)

I am getting this error, any idea?

cd /var/discourse  
./launcher enter app

It is stuck here!

 ![image](https://global.discourse-cdn.com/meta/original/3X/d/5/d539471f52fbeb25668422f9cb6f50f4be82ecef.png)

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [August 20, 2020, 4:22pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/60 "2020-08-20T16:22:11Z")

</div>

Hard to tell with the blurring, but can you double check that there is an open quote `'` before the first username? It appears that the console is expecting more input (which may indicate an open string)

---

<div class="post-metadata">

### Author: ![yahyagenc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yahyagenc/32/188138_2.png) [@yahyagenc](https://meta.discourse.org/u/yahyagenc)
#### Post date: [October 2, 2020, 1:18pm UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/61 "2020-10-02T13:18:08Z")

</div>

```plaintext
> /var/www/discourse# rake users:merge['anon94132497','PavelCherenkovv'] --trace
** Invoke users:merge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute users:merge
rake aborted!
ArgumentError: ArgumentError
/var/www/discourse/app/services/post_owner_changer.rb:12:in `initialize'
/var/www/discourse/app/services/user_merger.rb:63:in `new'
/var/www/discourse/app/services/user_merger.rb:63:in `change_post_owner'
/var/www/discourse/app/services/user_merger.rb:51:in `block in move_posts'
/var/www/discourse/app/services/user_merger.rb:49:in `each'
/var/www/discourse/app/services/user_merger.rb:49:in `move_posts'
/var/www/discourse/app/services/user_merger.rb:13:in `merge!'
/var/www/discourse/lib/tasks/users.rake:50:in `block in <main>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:279:in `block in execute'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:279:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:279:in `execute'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:188:in `invoke'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:160:in `invoke_task'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block in top_level'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:125:in `run_with_threads'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:110:in `top_level'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:83:in `block in run'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:80:in `run'
bin/rake:13:in `<top (required)>'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/exe/bundle:46:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/exe/bundle:34:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => users:merge

```

I got this error?

---

<div class="post-metadata">

### Author: ![lavrenkov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lavrenkov/32/234205_2.png) [@lavrenkov](https://meta.discourse.org/u/lavrenkov)
#### Post date: [July 21, 2021, 11:42am UTC](https://meta.discourse.org/t/rake-task-for-merging-users/89086/62 "2021-07-21T11:42:59Z")

</div>

Hi.  
Found a minor bug after a merge. A Welcome badge is displayed twice in the user profile like if he got it twice for the first time 🙂

[Previous page](https://meta.discourse.org/t/rake-task-for-merging-users/89086.md?page=2)

[Next page](https://meta.discourse.org/t/rake-task-for-merging-users/89086.md?page=4)
