# Script: sprite.layers

**URL:** https://community.aseprite.org/t/script-sprite-layers/7051
**Category:** Help
**Created:** [September 27, 2020, 11:51am UTC](https://community.aseprite.org/t/script-sprite-layers/7051 "2020-09-27T11:51:45Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![dacap](https://community.aseprite.org/user_avatar/community.aseprite.org/dacap/32/10_2.png) [@dacap](https://community.aseprite.org/u/dacap)
#### Post date: [September 27, 2020, 3:29pm UTC](https://community.aseprite.org/t/script-sprite-layers/7051/2 "2020-09-27T15:29:33Z")

</div>

Hi @Olga_Galvanova, this is a common problem when we have to remove elements from a collection that is being iterated. In this case the problem is that we cannot go through the `sprite.layers` collection and modify it at the same time.

My recommendation would be to create a table of layers first, and then iterate that table:

```lua
local sprite = app.activeSprite
local layers = {}
for i, layer in ipairs(sprite.layers) do
  table.insert(layers, layer)
end
for i, layer in ipairs(layers) do
  if not layer.isVisible then
    app.activeLayer = layer
    app.command.RemoveLayer()
  end
end

```

---

_[View the full topic](https://community.aseprite.org/t/script-sprite-layers/7051)._
