Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

how to remove old registered hosts from DB

avatar

I am trying to remove old worker from the DB ( from ambari-server machine ) as the following

* reference - https://community.hortonworks.com/questions/52910/how-can-i-remove-registered-host-but-do-not-added....

psql -U ambari ambari

Password for user ambari:
ambari-> select host_id from hosts where host_name='worker05.sys59.com';

host_id 

---------

255 

(1 row)

so I delete the host:

ambari=> Delete from hoststate where host_id=255
ambari-> Delete from hosts where host_id=255
ambari=> select host_id from hosts where host_name='worker05.sys59.com';
host_id ---------
255 (1 row)

this worker still exists in DB , ( host_id=255 ) , even I restart the ambari-server !!

why?

and how to total remove this worker include all worker properties from postgreSQL ?

Michael-Bronson
1 ACCEPTED SOLUTION

avatar
Master Mentor

@Michael Bronson

You can try the following approach to completely remove the unwanted host from your ambari Database.


0. Stop ambari-server.

# ambari-server stop

1. Please take a fresh ambari DB dump for safety and backup.

2. Now run the following SQL queries inside your ambari DB to delete the unwanted host. Please replace the "unwanted1.host.com" with your unwanted hostname and similarly the "351" with the "host_id" that you want to remove from your Database.

delete from execution_command where task_id in (select task_id from host_role_command where host_id in (351));
delete from host_version where host_id in (351);
delete from host_role_command where host_id in (351);
delete from serviceconfighosts where host_id in (351);
delete from hoststate where host_id in (351);
delete from kerberos_principal_host WHERE host_id='unwanted1.host.com';  ----> For kerberized Env
delete from hosts where host_name in ('unwanted1.host.com');
delete from alert_current where history_id in ( select alert_id from alert_history where host_name in ('unwanted1.host.com'));

3. Now restart ambari-server.

# ambari-server start

.

View solution in original post

15 REPLIES 15

avatar
Super Guru

I strongly suggest not to delete/edit entries directly in database. However if you still want to do it at your own risk

delete from host_role_command where host_id=351;
Delete from hosts where host_id=351;

avatar
Super Guru

@Michael Bronson,

You have to delete all the rows which have FK constraint in different tables. Then you will be able to delete the host entry

avatar
Super Guru

@Michael Bronson,

I'm sorry. I do not have a setup where I can try this currently. But you can see the error message and delete the rows of a table on which it complains the FK constraint

for ex: ERROR: update or delete on table "host_role_command" violates foreign key constraint "fk_execution_command_task_id" on table "execution_command" DETAIL: Key (task_id)=(5156) is still referenced from table "execution_command".

For this error you can run

delete from execution_command where task_id=5156;

avatar

we still get the same errors about

ambari=> delete from host_role_command where host_id=351;
ERROR:  update or delete on table "host_role_command" violates foreign key constraint "fk_execution_command_task_id" on table "execution_command"
DETAIL:  Key (task_id)=(5156) is still referenced from table "execution_command".
ambari=> Delete from hosts where host_id=351;
ERROR:  update or delete on table "hosts" violates foreign key constraint "fk_host_role_command_host_id" on table "host_role_command"
DETAIL:  Key (host_id)=(351) is still referenced from table "host_role_command".
Michael-Bronson

avatar

can you share with me please how to rows which have FK constraint in different tables

Michael-Bronson

avatar
Master Mentor

@Michael Bronson

You can try the following approach to completely remove the unwanted host from your ambari Database.


0. Stop ambari-server.

# ambari-server stop

1. Please take a fresh ambari DB dump for safety and backup.

2. Now run the following SQL queries inside your ambari DB to delete the unwanted host. Please replace the "unwanted1.host.com" with your unwanted hostname and similarly the "351" with the "host_id" that you want to remove from your Database.

delete from execution_command where task_id in (select task_id from host_role_command where host_id in (351));
delete from host_version where host_id in (351);
delete from host_role_command where host_id in (351);
delete from serviceconfighosts where host_id in (351);
delete from hoststate where host_id in (351);
delete from kerberos_principal_host WHERE host_id='unwanted1.host.com';  ----> For kerberized Env
delete from hosts where host_name in ('unwanted1.host.com');
delete from alert_current where history_id in ( select alert_id from alert_history where host_name in ('unwanted1.host.com'));

3. Now restart ambari-server.

# ambari-server start

.