[ZBX-25284] Not possible to delete inherited host prototype from host if a web scenario was added after discovery of a host by this prototype Created: 2024 Sep 24  Updated: 2025 Dec 06  Resolved: 2025 Dec 06

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: API (A), Frontend (F), Installation (I)
Affects Version/s: 6.4.20rc1, 7.0.5rc1, 7.2.0alpha1
Fix Version/s: 7.0.22rc1, 7.4.6rc1, 8.0.0alpha2 (master)

Type: Problem report Priority: Trivial
Reporter: Sergejs Olonkins Assignee: Janis Freibergs
Resolution: Fixed Votes: 0
Labels: delete, host.prototypes, inheritance
Remaining Estimate: Not Specified
Time Spent: 48.25h
Original Estimate: Not Specified

Attachments: GIF File cant_delete_host_prototype.gif    
Issue Links:
Causes
caused by ZBXNEXT-6980 Implement audit log for item Closed
Duplicate
is duplicated by ZBX-26997 Cannot delete Discovery rule with hos... Closed
Sub-task
part of ZBX-27205 Web scenarios migrated from Zabbix 6 ... Closed
Team: Team C
Sprint: S24-W40/41, S24-W42/43, S24-W46/47, S24-W48/49, S24-W50/51/52/1, S25-W2/3, S25-W26/27, S25-W38/39, S25-W46/47, S25-W48/49
Story Points: 2

 Description   

Problem description: Imagine that there's a template has a host prototype that should link the discovered hosts to the same template. This template is linked to a host "test", a host is discovered by this prototype. Afterwards, a Web scenario is added to this template, and, therefore it appears on the discovered host and on the "test" host, and then the template is unlinked from host "test". After these actions it will not be possible to delete this host prototype from host "test" due to the following error:

 pg_query(): Query failed: ERROR: update or delete on table "hosts" violates foreign key constraint "c_httptest_2" on table "httptest"
DETAIL: Key (hostid)=(10643) is still referenced from table "httptest". [host_prototypes.php:325 → CApiWrapper->__call() → CFrontendApiWrapper->callMethod() → CApiWrapper->callMethod() → CFrontendApiWrapper->callClientMethod() → CLocalApiClient->callMethod() → CHostPrototype->delete() → CHostPrototype::deleteForce() → DB::delete() → DBexecute() → pg_query() → CConfigFile->{closure}() in conf/zabbix.conf.php:87]
Error in query [DELETE FROM hosts WHERE hostid='10643'] [ERROR: update or delete on table "hosts" violates foreign key constraint "c_httptest_2" on table "httptest"
DETAIL: Key (hostid)=(10643) is still referenced from table "httptest".]
SQL statement execution has failed "DELETE FROM hosts WHERE hostid='10643'"

Steps to reproduce:

  1. Create a template with an LLD rule of type trapper
  2. Add a host prototype to this LLD rule that would link the discovered hosts to this created template
  3. Create a host and link the created template to it
  4. Execute the discovery rule.
  5. Make sure that host is discovered from the host prototype and that it is linked to the created template
  6. Add a web scenario to the template.
  7. Make sure that the web scenario is added both to the created host and to the discovered host
  8. Unlink template from the created host
  9. Attempt to delete the host prototype from the created host.

Result: host prototype cannot be deleted due to the above mentioned error.
Expected: host prototype should be successfully deleted.

Example:



 Comments   
Comment by Janis Freibergs [ 2024 Oct 11 ]

Fixed in development branches:

Comment by Tristan Pct [ 2025 Nov 13 ]

This bug persist in 7.4.5 if the host was created before 7.0

Is there a way to delete this host?

Cannot delete hostpg_query(): Query failed: ERROR:  update or delete on table "hosts" violates foreign key constraint "c_httptest_2" on table "httptest"
DETAIL:  Key (hostid)=(12163) is still referenced from table "httptest". [zabbix.php:17 → require_once() → ZBase->run() → ZBase->processRequest() → CController->run() → CControllerHostMassDelete->doAction() → CApiWrapper->__call() → CFrontendApiWrapper->callMethod() → CApiWrapper->callMethod() → CApiWrapper->callClientMethod() → CLocalApiClient->callMethod() → CHost->delete() → CHost::deleteForce() → CDiscoveryRule::deleteForce() → CDiscoveryRuleGeneral::deleteAffectedHostPrototypes() → CHostPrototype::deleteForce() → DB::delete() → DB::deleteByPkIds() → DB::deleteByIdField() → DBexecute() → pg_query() in include/db.inc.php:255]Error in query [DELETE FROM hosts WHERE hostid IN (11298,12163)] [ERROR:  update or delete on table "hosts" violates foreign key constraint "c_httptest_2" on table "httptest"
DETAIL:  Key (hostid)=(12163) is still referenced from table "httptest".]SQL statement execution has failed "DELETE FROM hosts WHERE hostid IN (11298,12163)" 
Comment by Vladimirs Maksimovs [ 2025 Nov 13 ]

The main issue is that by mistake web scenarios are inherited from templates to host prototypes. If web scenarios on host prototypes are deleted, you will be able to delete a host normally.

It is possible to realize using the following workaround:

  1. Gather IDs of all host prototypes to which is linked a template with web scenario.
  2. Make the following SQL query to get the IDs of the web scenarios of host prototype:
    SELECT ht.httptestid FROM httptest ht WHERE ht.hostid IN (<gathered IDs of host prototypes>);
    
  1. Unset relation of the found web scenarios with template using the following SQL query:
    UPDATE httptest SET templateid=NULL WHERE templateid IN (<IDs of found web scenarios>);
    
  1. Call httptest.delete API method by specifying the IDs of web scenarios.
    {
        "jsonrpc": "2.0",
        "method": "httptest.delete",
        "params": [<IDs of web scenarios>],
        "id": 1
    }
    

After these steps it will be possible to delete the host prototype, its LLD rule, or host.

Comment by Tristan Pct [ 2025 Nov 13 ]

Thank you, this workaround works for me!

There is just a small mistake in the 2nd query, it must be:

UPDATE httptest SET templateid=NULL WHERE httptestid IN (<IDs of found web scenarios>); 
Comment by Vladimirs Maksimovs [ 2025 Nov 13 ]

Thank you! I corrected it.

Comment by Janis Freibergs [ 2025 Dec 02 ]

Available in versions:

Generated at Fri Jan 09 02:18:51 EET 2026 using Jira 10.3.13#10030013-sha1:56dd970ae30ebfeda3a697d25be1f6388b68a422.