How to delete unmanaged HOST with foreman-rake

How can I delete server 33 ?

We tried the following.

foreman-rake console

Loading production environment (Rails 6.1.7.3)
irb(main):001:0> Host::Base.where(id: [33]).destroy_all
Traceback (most recent call last):
2: from lib/tasks/console.rake:5:in `block in <top (required)>’
1: from (irb):1
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table “hosts” violates foreign key constraint “fk_rails_a82ac722f1” on table “host_facets_reported_data_facets”)
DETAIL: Key (id)=(33) is still referenced from table “host_facets_reported_data_facets”.

and then did this

irb(main):002:0> HostFacets::ReportedDataFacet.find_by_host_id(33).destroy
=> #<HostFacets::ReportedDataFacet id: 33, host_id: 33, boot_time: “2023-06-22 14:54:35.000000000 +0000”, created_at: “2023-06-22 17:53:56.061388000 +0000”, updated_at: “2023-06-22 17:53:56.061388000 +0000”, virtual: true, sockets: 1, cores: 16, ram: 126926, disks_total: 375809638400, kernel_version: “4.18.0-477.13.1.el8_8.x86_64”, bios_vendor: “Amazon EC2”, bios_release_date: “10/16/2017”, bios_version: “1.0”>

But then we get this

irb(main):003:0> Host::Base.where(id: [33]).destroy_all
Traceback (most recent call last):
2: from lib/tasks/console.rake:5:in `block in <top (required)>’
1: from (irb):3
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table “hosts” violates foreign key constraint “fk_rails_cadab260b9” on table “host_puppet_facets”)
DETAIL: Key (id)=(33) is still referenced from table “host_puppet_facets”.

I cant work out how to get rid of that reference.

Is there any reason you try to do this via foreman-rake?
foreman-rake is (as far as I understand) intended to be a developer and “last-resort” troubleshooting tool, not as a everyday production tool. I would recommend using either hammer, the API or the GUI to delete hosts. In my experience, neither of these cause any problems when deleting hosts (managed or not), as long as you don’t have any general problems in your database.

Hi - thanks for that - Yes I deleted the host using the foreman GUI.

And it doesn’t appear in any hammer commands. Such as “hammer host list”

But when I tried to “re-add” the new server and do anything with it in the GUI - it complains that the hostname is already in use.

So somehow the GUI delete left this entry behind, which is why I am trying to clean it up using the foreman-rake command, or any other method that works to be honest.

We have encountered similar problems in the past, in our case we could delete the “stale” host via GUI by setting the context to “Any Org”/“Any Loc” and then we could find the host and delete it via the regular GUI methods.
From our experience, this problem is probably related to Puppet or RHSM Fact uploads not being mapped to the correct host for some reason. Since we have encountered this behavior with hosts that were still in Foreman, too, effectively causing duplicate hosts, this is probably not related to host deletion but has been a prior problem that you just did not notice.
In our case, this stopped after we turned “Create new host when facts are uploaded” off in the settings, since we don’t need that feature.

Thanks - tried that and the host still doesn’t appear in the GUI

Is anyone able to assist with the required rake command ?

I have exactly the same problem over here. I now at least know where the corruption in the DB is, but I cannot repair it due to the violated key constraint. I really need to solve this to get the hosts configurable again.
A funky workaround for some stuff is to just select the host from the host list and use a bulk operation (e.g. change environment). That succeeds although the basic operations on the host-specific GUI fail.

Ok, solved with a little more help of: HTTP error (422 - Unknown): Validation failed: Name has already been taken - #7 by marcelo.messias

There it was solved by simply manually deleting the corresponding blocking data entities. So I did this as well and was then able to delete the host.
So first I identified the odd host:

sudo -u postgres psql foreman -c "SELECT id FROM hosts WHERE name = 'odd.host.name';"

Then I tried to delete the host:

foreman-rake console
Host::Base.where(id: [16]).destroy_all

This spit out the foreign key constraint which blocked the operation. Then in my case I had to delete from the following two tables:

sudo -u postgres psql foreman -c "DELETE FROM host_puppet_facets WHERE host_id = 16;"
sudo -u postgres psql foreman -c "DELETE FROM host_puppet_facets WHERE host_id = 16;"

Now the above mentioned delete operation from the rake-console was successful.