   parts_array int[] := '{}';
   address_array int[] := '{}';
 BEGIN
-  -- delete parts
-  WITH RECURSIVE part(id, building_parent_id, level) AS (
-    SELECT id, building_parent_id, 1 AS level 
-      FROM building b, (
-        SELECT unnest($1) AS b_id
-      ) a
-      WHERE b.id = a.b_id
-    UNION ALL
-      SELECT b.id, b.building_parent_id, p.level + 1 AS level 
-        FROM building b, part p
-        WHERE b.building_parent_id = p.id
-  )
-  SELECT array_agg(bp.g_id) INTO parts_array FROM (
-    SELECT unnest(citydb_pkg.delete_buildings(batch.ids)) AS g_id FROM (
-      SELECT array_agg(id) AS ids
-        FROM part
-        WHERE level > 1
-        GROUP BY level ORDER BY level DESC
-    ) batch
-  ) bp;
+  -- delete building parts
+  PERFORM citydb_pkg.delete_buildings(b.id) INTO parts_array
+    FROM building b, (
+      SELECT unnest($1) AS b_id
+    ) a   
+    WHERE b.building_parent_id = b_id
+      AND b.id != b_id;
 
   -- delete rooms
   PERFORM citydb_pkg.delete_rooms(r.id)

      SELECT unnest($1) AS co_id
    ) a
    WHERE sg.cityobject_id = a.co_id
  UNION ALL
    SELECT sg.id, sg.parent_id, g.level + 1 AS level 
      FROM surface_geometry sg, geometry g
      WHERE sg.parent_id = g.id
)
SELECT array_agg(g.g_id) AS result_array FROM (
  SELECT unnest(citydb_pkg.batch_delete_geometry(batch.ids)) AS g_id FROM (
    SELECT array_agg(id) AS ids
      FROM geometry
      GROUP BY level ORDER BY level DESC
  ) batch
) g;
$$
LANGUAGE sql STRICT;


  -- delete entries from surface_geometry table
  WITH delete_geometry AS (
    DELETE FROM surface_geometry sg USING (
      SELECT sg_id, idx FROM unnest($1) WITH ORDINALITY AS arr(sg_id, idx)
    ) a
    WHERE sg.id = a.sg_id
    RETURNING sg.id
