# Properly delete actor from level

**URL:** <https://forums.unrealengine.com/t/properly-delete-actor-from-level/478135>\
**Category:** C++\
**Tags:** Destroy-Actor, unreal-engine, CPP, question\
**Created:** [February 2, 2021, 4:51pm UTC](https://forums.unrealengine.com/t/properly-delete-actor-from-level/478135 "2021-02-02T16:51:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![efevi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@efevi](https://forums.unrealengine.com/u/efevi)\
**Post date:** [February 2, 2021, 4:51pm UTC](https://forums.unrealengine.com/t/properly-delete-actor-from-level/478135/1 "2021-02-02T16:51:23Z")

</div>

I am trying to delete an actor from a level with this:

```
world->ContainsActor(actor){
   world->DestroyActor(actor, true, true);
}

```

And immediately after, I am trying to spawn the actor with same name with related actor factory.

I get error like this:

An actor of name 'SomeActorName already exists in level ‘Level SomeLevelPath’.

Following seems to fix the problem. But if I change actor settings in the editor and re-run the same code again, I still get the error:

```
    world->ContainsActor(actor){
      	world->DestroyActor(actor, true, true);
	      world->ForceGarbageCollection(true);
	      world->PerformGarbageCollectionAndCleanupActors();
    } 

```

What is the proper way to delete an actor from a level, so that i can immediately after spawn an actor with the same name?

---

<div class="post-metadata">

**Author:** ![Detach789](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Detach789](https://forums.unrealengine.com/u/Detach789)\
**Post date:** [February 2, 2021, 5:13pm UTC](https://forums.unrealengine.com/t/properly-delete-actor-from-level/478135/2 "2021-02-02T17:13:08Z")

</div>

I don’t do a lot of C++, so I could be very wrong here… but I could have sworn the language to destroy was:

YourActorHere-\>Destroy();

Like, you get the actor and then call the destroy function. I don’t think the name of the actor goes in the parentheses. Does that not work?

---

<div class="post-metadata">

**Author:** ![efevi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@efevi](https://forums.unrealengine.com/u/efevi)\
**Post date:** [February 2, 2021, 5:38pm UTC](https://forums.unrealengine.com/t/properly-delete-actor-from-level/478135/3 "2021-02-02T17:38:02Z")

</div>

Thank you for your quick answer,

Actually what “actor-\>Destroy()” does is inside the function: “actor-\>GetWorld()-\>DestroyActor()”. So they effectively seem to do the same thing. I tried it and the result is same.

It seems to be about garbage collection. Forcing garbing collection does not seem the work in some cases as in my original post.

I think I will solve my problems by just enumerating actor names like Actor001, Actor002. And I will let the garbage collection collect its garbage whenever it desires.

---

<div class="post-metadata">

**Author:** ![Detach789](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Detach789](https://forums.unrealengine.com/u/Detach789)\
**Post date:** [February 2, 2021, 5:45pm UTC](https://forums.unrealengine.com/t/properly-delete-actor-from-level/478135/4 "2021-02-02T17:45:07Z")

</div>

Well, thank you for teaching me something then!

---

<div class="post-metadata">

**Author:** ![gkbell](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gkbell](https://forums.unrealengine.com/u/gkbell)\
**Post date:** [May 24, 2022, 12:35pm UTC](https://forums.unrealengine.com/t/properly-delete-actor-from-level/478135/5 "2022-05-24T12:35:53Z")

</div>

I am facing similar problem. Do we have any resolution figured out ?  
Thanks for the help in advance !
