# Is there a way to only destroy the oldest sprite in a group of sprites of the same name?

**URL:** <https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546>\
**Category:** Help\
**Created:** [November 26, 2023, 3:19am UTC](https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546 "2023-11-26T03:19:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brainstorm\_Studios](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/brainstorm_studios/32/20476_2.png) [@Brainstorm\_Studios](https://forum.makecode.com/u/Brainstorm_Studios)\
**Post date:** [November 26, 2023, 3:19am UTC](https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546/1 "2023-11-26T03:19:33Z")

</div>

here’s the deal. i want to know how to destroy the sprite of a specific name that’s been onscreen the longest, instead of the latest one.

something like:

[destroy oldest sprite (sprite variable name)]

as opposed to…

[destroy (variable name)]

if this is an option, i would appreciate knowing where i can find it, if not, it would be great if someone could make an extension for it. thank you, and stay creative!

---

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [November 27, 2023, 5:04pm UTC](https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546/2 "2023-11-27T17:04:44Z")

</div>

Idea to implement:

1. On sprite creation, use sprite data to assign the current game ms.
2. To destroy the oldest sprite just iterate over all of them or the type you want and just destroy the sprite data with the smallest game time, as that would be the oldest

---

<div class="post-metadata">

**Author:** ![Mega0star01](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mega0star01/32/14784_2.png) [@Mega0star01](https://forum.makecode.com/u/Mega0star01)\
**Post date:** [November 27, 2023, 5:04pm UTC](https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546/3 "2023-11-27T17:04:51Z")

</div>

u could use a timer and put it just after the “make my sprite” block.

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [November 27, 2023, 5:57pm UTC](https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546/4 "2023-11-27T17:57:54Z")

</div>

Try accessing the first element of the `get all sprites of kind` block. I believe they are placed there in order of creation. Once you define a sprite (eg. mySprite), that sprite is now assigned to the variable. When you assign it again, the variable is now the new sprite, and the old one persists in the game, but is no longer assigned.

Here’s an example.

> **[Destroy oldest sprite](https://arcade.makecode.com/S81156-87967-74277-04514)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

(press A to destroy a sprite)

I hope this helps, if you have any more questions, please ask!

---

<div class="post-metadata">

**Author:** ![ChimbroDaPro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/chimbrodapro/32/15852_2.png) [@ChimbroDaPro](https://forum.makecode.com/u/ChimbroDaPro)\
**Post date:** [November 27, 2023, 6:34pm UTC](https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546/5 "2023-11-27T18:34:05Z")

</div>

Try adding the sprites to an array as they’re created, then destroy the sprite with an index of 0.

---

<div class="post-metadata">

**Author:** ![Blobbey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/blobbey/32/20831_2.png) [@Blobbey](https://forum.makecode.com/u/Blobbey)\
**Post date:** [November 27, 2023, 6:36pm UTC](https://forum.makecode.com/t/is-there-a-way-to-only-destroy-the-oldest-sprite-in-a-group-of-sprites-of-the-same-name/24546/6 "2023-11-27T18:36:17Z")

</div>

![Screenshot 2023-11-27 083053](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/e/ee05432fdd4bb25da1fa3e3f1e3332a1b9a05802.png)  
This should work. 🙂
