[BUGFIX] some unreferenced elements were not deleted

I changed how the referenced elements are retrieved (by doing the same as in the templavoilla index.php of the BE module)