Remove Elements in dgn-file with specific name

I have this:

using (FileLevelCache levelCache = ActiveDgnFile.GetLevelCache())
{
LevelHandleCollection levelHandles = levelCache.GetHandles();
foreach (LevelHandle level in levelHandles)
{
if (level.Name.EndsWith(" P") || level.Name.EndsWith(" L"))
{
levelCache.RemoveLevel(null, level);

levelCache.Write();
}
}
}

But it dont remove the levels. I think I need to remove the elements assosiated with the levels before removing the levels. But I dont know how...

Can anyone help?

Parents Reply Children
  •             using (FileLevelCache levelCache = ActiveDgnFile.GetLevelCache())
                {
                    
                    foreach (DgnModel dgnModel in ActiveDgnFile.GetLoadedModelsCollection())
                    {
                        ModelElementsCollection elementsCollection = dgnModel.GetGraphicElements();
                        LevelHandleCollection levelHandles = levelCache.GetHandles();
    
                        foreach (LevelHandle level in levelHandles)
                        {
                            if (level.Name.EndsWith(" P") || level.Name.EndsWith(" L"))
                            {
                                foreach (Element el in elementsCollection)
                                {
                                    el.DeleteFromModel();
                                }
    
                            }
                        }
                    }
                }