# Trying to clear a layout but getting stuck at nested GridLayouts

**URL:** <https://discourse.julialang.org/t/trying-to-clear-a-layout-but-getting-stuck-at-nested-gridlayouts/123269>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [November 29, 2024, 11:20pm UTC](https://discourse.julialang.org/t/trying-to-clear-a-layout-but-getting-stuck-at-nested-gridlayouts/123269 "2024-11-29T23:20:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AMB](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amb/32/211738_2.png) [@AMB](https://discourse.julialang.org/u/AMB)\
**Post date:** [November 29, 2024, 11:20pm UTC](https://discourse.julialang.org/t/trying-to-clear-a-layout-but-getting-stuck-at-nested-gridlayouts/123269/1 "2024-11-29T23:20:48Z")

</div>

I’m trying to delete the layout altogether with the code, below, because I want to regenerate the content in response to input from the user. `clear_layout(fig.layout)` successfully removes all the Axes, Labels, etc, but I’m stumped when it comes to removing the nested GridLayouts. You can see that my code recurses into them and it does successfully remove blocks in those nested GridLayouts, but how can I get rid of the GridLayouts themselves?

```julia
    function clear_layout(layout::gm.GridLayout)
        items_to_remove = []
        for x in gm.contents(layout)
            if typeof(x) == gm.GridLayout
                clear_layout(x)
            else
                push!(items_to_remove, x)
            end
        end
        map(delete!, items_to_remove)
    end

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 30, 2024, 5:33pm UTC](https://discourse.julialang.org/t/trying-to-clear-a-layout-but-getting-stuck-at-nested-gridlayouts/123269/4 "2024-11-30T17:33:57Z")

</div>

There’s no public function to remove a gridlayout, I somehow never got to it. The function to remove a child content in `GridLayoutBase` is `remove_from_gridlayout!` but you need to pass the `gridcontent` wrapper of the respective object which would be a bit roundabout for a public function.

Here’s an example that shows that a nested `GridLayout` can be removed, but which also demonstrates that the objects that were placed in that layout are still in the `Figure` and have to be `delete!`d manually (being in the Figure and being connected to a layout are two orthogonal things). They just stop being updated as the addition of the `Box` shows.

```julia
GLB = Makie.GridLayoutBase

f = Figure()
Axis(f[1, 1])
gl = GridLayout(f[1, 2])
for i in 1:3, j in 1:3
    Axis(gl[i, j])
end
GLB.remove_from_gridlayout!(GLB.gridcontent(gl))
Box(f[2, 1:2])
f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/6/4/64ce642a621c2d8e1a5b1b67d86508e21bf4b9d0.png)

At some point I will probably make some unified interface for deleting stuff including GridLayouts from a Figure.

---

<div class="post-metadata">

**Author:** ![AMB](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amb/32/211738_2.png) [@AMB](https://discourse.julialang.org/u/AMB)\
**Post date:** [November 30, 2024, 6:21pm UTC](https://discourse.julialang.org/t/trying-to-clear-a-layout-but-getting-stuck-at-nested-gridlayouts/123269/5 "2024-11-30T18:21:23Z")

</div>

Thanks… I was reading about `remove_from_gridlayout!` in another thread but it didn’t make sense until your reply, above. This now works for me:

```julia
function clear_layout(layout::gm.GridLayout)

    # Begin by removing the blocks from the recursive GridLayout structure
    items_to_remove = []
    for block in gm.contents(layout)
        if typeof(block) == gm.GridLayout
            clear_layout(block)
        else
            push!(items_to_remove, block)
        end
    end
    map(delete!, items_to_remove)

    # Now remove the GridLayout substructure
    items_to_remove = []
    for wrapper in layout.content
        push!(items_to_remove, wrapper) 
    end         
    map(glb.remove_from_gridlayout!, items_to_remove)

    # Finally, trim so that the layout is 1 by 1.
    gm.trim!(layout)
end

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 30, 2024, 6:33pm UTC](https://discourse.julialang.org/t/trying-to-clear-a-layout-but-getting-stuck-at-nested-gridlayouts/123269/6 "2024-11-30T18:33:43Z")

</div>

Glad you got it to work! As a side note, `foreach` is like `map` without storing the results in a vector

---

<div class="post-metadata">

**Author:** ![AMB](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amb/32/211738_2.png) [@AMB](https://discourse.julialang.org/u/AMB)\
**Post date:** [November 30, 2024, 7:37pm UTC](https://discourse.julialang.org/t/trying-to-clear-a-layout-but-getting-stuck-at-nested-gridlayouts/123269/7 "2024-11-30T19:37:09Z")

</div>

😂 I was too lazy to look up `foreach`. Thanks!
