The Lowermost position should be deleted first
DECLARE
ln_ovn NUMBER := 1;
CURSOR c
IS
SELECT LPAD (' ', 5 * LEVEL) || has.NAME HIERARCHY, LEVEL,
hap.NAME parent_name, pse.parent_position_id,
has.NAME child_name, pse.subordinate_position_id,
pse.pos_structure_element_id, pse.object_version_number
FROM (SELECT NAME, position_id
FROM hr_all_positions_f_tl
WHERE LANGUAGE = USERENV ('LANG')) hap,
(SELECT NAME, position_id
FROM hr_all_positions_f_tl
WHERE LANGUAGE = USERENV ('LANG')) has,
per_pos_structure_elements pse
WHERE pse.business_group_id = 141
AND hap.position_id = pse.parent_position_id
AND has.position_id = pse.subordinate_position_id
--start with pse.parent_position_id =
CONNECT BY PRIOR pse.subordinate_position_id = pse.parent_position_id
AND PRIOR pse.pos_structure_version_id =
pse.pos_structure_version_id
AND PRIOR pse.business_group_id = pse.business_group_id
ORDER BY 2 DESC, 4;
BEGIN
FOR i IN c
LOOP
BEGIN
ln_ovn := i.object_version_number;
hr_pos_hierarchy_ele_api.delete_pos_hierarchy_ele
(p_validate => FALSE,
p_pos_structure_element_id => i.pos_structure_element_id,
p_object_version_number => ln_ovn,
p_hr_installed => 'I'
);
EXCEPTION
WHEN OTHERS
THEN
NULL;
END;
END LOOP;
END;