Can not Delete User in Zammad console

I tried step-1 to step-3 in the link (https://docs.zammad.org/en/latest/admin/console/dangerzone-for-experts.html#deleting-customers) and I got an error as shown below.

I try to remove the user directly by User.find(3).destroy(), but no luck to delete it. Could you get any clue out of it?

$ sudo zammad run rails c
Loading production environment (Rails 5.2.4.1)
irb(main):001:0> User.find(3)
=> #<User id: 3, organization_id: nil, login: “dinesh12@gmail.com”, firstname: “Dinesh”, lastname: “Admin”, email: “dinesh12@gmail.com”, image: nil, image_source: nil, web: “”, password: “$argon2id$v=19$m=65536,t=2,p=1$VorELCR1hVGERJRUVvv…”, phone: “”, fax: “”, mobile: “”, department: “”, street: “”, zip: “”, city: “”, country: “”, address: “”, vip: false, verified: false, active: false, note: “”, last_login: “2020-05-14 12:46:31”, source: nil, login_failed: 0, out_of_office: false, out_of_office_start_at: nil, out_of_office_end_at: nil, out_of_office_replacement_id: nil, preferences: {“notification_config”=>{“matrix”=>{“create”=>{“criteria”=>{“owned_by_me”=>true, “owned_by_nobody”=>true, “no”=>false}, “channel”=>{“email”=>true, “online”=>true}}, “update”=>{“criteria”=>{“owned_by_me”=>true, “owned_by_nobody”=>true, “no”=>false}, “channel”=>{“email”=>true, “online”=>true}}, “reminder_reached”=>{“criteria”=>{“owned_by_me”=>true, “owned_by_nobody”=>false, “no”=>false}, “channel”=>{“email”=>true, “online”=>true}}, “escalation”=>{“criteria”=>{“owned_by_me”=>true, “owned_by_nobody”=>false, “no”=>false}, “channel”=>{“email”=>true, “online”=>true}}}}, “locale”=>“en-us”, “intro”=>true, “tickets_closed”=>5, “tickets_open”=>5, “chat”=>{“active”=>{“1”=>“on”}}}, updated_by_id: 13, created_by_id: 1, created_at: “2020-01-21 15:27:30”, updated_at: “2020-07-20 13:21:41”>
irb(main):002:0> User.find(3).destroy()

curl -X delete “http://127.0.0.1:9200/zammad_production_stats_store/_doc/1

200

Traceback (most recent call last):
1: from (irb):2
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table “users” violates foreign key constraint “fk_rails_45307c95a3” on table “users”)
DETAIL: Key (id)=(3) is still referenced from table “users”.
: DELETE FROM “users” WHERE “users”.“id” = $1

Steps to reproduce the behavior:

  • Create an account in Admin user management
  • Create Tickets and assign the User
  • Remove the Ticket and User

First of all, ignoring the issue template is not that good. We usually need these information to help you better.

User-ID 3 is an agent user which has tons of references.
While it technically is possible to remove those users, we currently do not officially support it.

The provided removal code is for customers*.

* No, downgrading an agent to a customer is technically not enough.

Helping you in this case to the full extend is out of scope, because removing an agent can be a lot of work. Agents and admins technically can leave a lot traces in Zammad which may lead you to fiddling for 15 minutes and more.

In your case, hopefully this is enough:

Ticket.where(owner_id: 3).update_all(owner_id: nil)

I doubt it’s enough because… well, it’s the first user which is the admin users so…

First of all sorry for the format, and I will correct it. Many thanks for your quick response.

Yes, you are right, the user was an Agent and I changed him to the customer for deleting them by using commands since I did not see any web UI to do it.
I got the following outcome while running the command, which you shared in the previous post.
irb(main):001:0> Ticket.where(owner_id: 3).update_all(owner_id: nil)
=> 0
irb(main):002:0>

Do you share the procedure to delete the agents in the command line or Admin GUI? I really thankful for your support :slight_smile:

It’s very hard to provide a generic solution for that.
As I already mentioned I always do that on requirenment. This happens like twice per year so I didn’t really bother. This will be soon be irrelevant anyway as soon as Zammad allows user deletion via UI soonish™.


To your problem, sorry, I miss read… it’s complaining about relations on the user table, not tickets. So User 3 of course did create or update users… ^^"

User.where(created_by_id: 3).update_all(created_by_id :1)
User.where(updated_by_id: 3).update_all(updated_by_id: 1)

Should help with that.