# How create a EntityCollection and remove

**URL:** <https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623>\
**Category:** CesiumJS\
**Created:** [October 28, 2018, 9:44pm UTC](https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623 "2018-10-28T21:44:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krzysztof\_Knop](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@Krzysztof\_Knop](https://community.cesium.com/u/Krzysztof_Knop)\
**Post date:** [October 28, 2018, 9:44pm UTC](https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623/1 "2018-10-28T21:44:33Z")

</div>

Hi,

I have a problem with adding Entity to the EntityCollection. I want to add them to the collection to later delete the whole collection with a single button.

var entities = viewer.entities;

var entity = new Cesium.Entity();  
var collection = new Cesium.EntityCollection();

function createEntity(name, x, y, z, description) {

entity = entities.add({

id : name,

position : Cesium.Cartesian3.fromDegrees(x,y,z),

name : name,

description : ‘description’,

billboard : {

image : ‘picture/logou/’+nazwa+’.png’,

sizeInMeters : true

}

});

collection.add(entity);

viewer.zoomTo(entity, new Cesium.HeadingPitchRange(0, 0, 1000));

}

function clear(){

collection.removeAll();

};

``

I am using Cesium 1.45.

---

<div class="post-metadata">

**Author:** ![Scott\_Reynolds](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/scott_reynolds/32/2014_2.png) [@Scott\_Reynolds](https://community.cesium.com/u/Scott_Reynolds)\
**Post date:** [October 29, 2018, 12:34pm UTC](https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623/2 "2018-10-29T12:34:04Z")

</div>

I think you are going to need a custom data source to hold your EntityCollection. See the Sandcastle “Custom DataSource” example under DataSources in the Gallery for inspiration.

Scott

---

<div class="post-metadata">

**Author:** ![Krzysztof\_Knop](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@Krzysztof\_Knop](https://community.cesium.com/u/Krzysztof_Knop)\
**Post date:** [October 29, 2018, 1:35pm UTC](https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623/3 "2018-10-29T13:35:23Z")

</div>

I have reviewed this example and I do not know how to use it in my case ;/

W dniu poniedziałek, 29 października 2018 13:34:04 UTC+1 użytkownik Scott Reynolds napisał:

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [October 30, 2018, 3:05pm UTC](https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623/4 "2018-10-30T15:05:43Z")

</div>

I think an easier way, if all you care about is keeping track of them and removing them later, is simply to add them to an array, and then loop over that array and remove all its items.

[Here’s a Sandcastle example](https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/#c=zVZbT9swFP4rVl9wReumaTtuBQ06JJBgQ4WNB8qDSU4bb44d2U4hoP73OTeapjAeBtP8UMk+37l95+ScjkCzOCTn1AREgxlT4cvwaxzegboE8HHX7fWbexMxEXOq0JzBPSi0jwTco1Gu+iN7wxtedh1JYSgToDZa6AkxMZVH8gHtoinlGtAiNZUaCpNjYZhhoK2xm9vMQS5hBSIANgtMcYmktmApdHHXRrEIbNCgGOWr8VyuyC6UjECZBD9NBLIH5iBGkktlYyoUsiu5Pjm9Oib3zASHPAoodsig2cp1pO+/qHJ09v0VDQURUGPxA+JMxGJJIKRZJ10bcU4lgYIGQn2/jFGBZ6iYcbAGiqf0eFIqnwmbmF5GMi6xZKpk+AVmCkDj9o5LnBZynfS3vf0pu2wRp4wvyyo23BbKmjIqhnXB6yytY6+ZbwKL7VdE2oyloWnVlkayNjNyTH1Ghcb9QTWgsCznbq2+OWRRELlsHRLFOsAFpXWO3T9zHEmezLLQKgwHDBRVXpDY50pHXeTYk1KKS0qosrWwmfSq5B8qRRN80+46WyXtlSTf8zy7cD/SRd5JvY9zsZMn0f1wD/ZjvG3+y0/grzrarXd0762OzmdkJd4X+xO3t52SjpIM4JxFujZvNITsnAmpDh9YOnF6TnpWqpRB6M9nyGAdot4aAu2+898NJtd9t8HUa5bLLS/do5ThlcS1QuYgu4CvWAg2djyNhZeGiJtFRaZS4Ww/2i5w9hgaVnYo4SBmJrCvm5vNSgXr3aIglHPAS8Ubdpt6LrLIcl1bzItWVtYU12g1htokHA5y8GcWRlIZFCuOCekYCCOerqfOXez9AkM8nSU27JRKQ5/NEfP3J43av4VJA3mcam0l05jzS/YIk8bBsGPxK2pc2gqJ2bc5KE6TFBJ0D87yR0LIsGOv61pGSn5HVcXibw) that will remove all the entities after a few seconds.

Let me know if that helps!

---

<div class="post-metadata">

**Author:** ![Krzysztof\_Knop](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@Krzysztof\_Knop](https://community.cesium.com/u/Krzysztof_Knop)\
**Post date:** [October 30, 2018, 8:04pm UTC](https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623/5 "2018-10-30T20:04:11Z")

</div>

Thank you very much for the hint of solving the problem, it helped me a lot. Out of curiosity, what was wrong with my code, where I added the “Entity” to the "EntityCollection? Maybe in the future I will need a collection 😕 It looks logical and easy in the documentation, but it does not work.

Thank you once again

---

<div class="post-metadata">

**Author:** ![Scott\_Reynolds](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/scott_reynolds/32/2014_2.png) [@Scott\_Reynolds](https://community.cesium.com/u/Scott_Reynolds)\
**Post date:** [October 31, 2018, 11:26am UTC](https://community.cesium.com/t/how-create-a-entitycollection-and-remove/7623/6 "2018-10-31T11:26:01Z")

</div>

The EntityCollection you created isn’t attached to the viewer/scene. That’s why I suggested the custom DataSource approach, because there is a mechanism to add that to the viewer while there isn’t one for adding an EntityCollection.
