# DestroyImmediate(Object, bool)

> Destroys the specified object immediately. Use with caution and in Edit mode only.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public static void DestroyImmediate(Object obj, bool allowDestroyingAssets)
```

### Parameters

**** (\[Object]\(/engine/6000.7/script-reference/unityengine/object)): Object to be destroyed.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Set to true to allow assets to be destroyed.

### Remarks

`DestroyImmediate` is intended for use in scripts that run in Edit mode, not at runtime. In Edit mode, the usual delayed destruction performed by `Destroy` does not occur, so immediate destruction is necessary.

In runtime code, use [Object.Destroy](/engine/6000.7/script-reference/unityengine/object/destroy.md) instead. This call marks the object for destruction at the end of the current frame, which is safer and prevents many common issues. If you only want to deactivate a GameObject, use [GameObject.SetActive](/engine/6000.7/script-reference/unityengine/gameobject/setactive.md) instead.

Attempting to call `DestroyImmediate` during physics trigger/contact events, animation event callbacks, rendering callbacks, or [MonoBehaviour.OnValidate()](/engine/6000.7/script-reference/unityengine/monobehaviour/onvalidate.md) results in an error.

Using `DestroyImmediate` with the optional `allowDestroyingAssets` parameter set to `true` permanently removes individual asset objects from the asset file. This operation cannot be undone. However, it does not delete the asset file itself from disk. If all objects within an asset file are destroyed, the asset file remains on disk but is empty. To delete an asset file from disk, use [AssetDatabase.DeleteAsset](/engine/6000.7/script-reference/unityeditor/assetdatabase/deleteasset.md) instead.

Destroying or removing objects from a collection (such as an array or list) while looping through it can cause skipped elements, out-of-bounds errors, or other unpredictable behavior. This is a general programming risk, not unique to Unity.

### Examples

```csharp
// Select one or more GameObjects in your hierarchy.
// Go to the menu: Tools > Destroy Selected GameObjects Immediately.
// The selected GameObjects will be deleted from the scene immediately.

using UnityEngine;
using UnityEditor;

public class DestroyImmediateExample
{
    [MenuItem("Tools/Destroy Selected GameObjects Immediately")]
    static void DestroySelectedGameObjects()
    {
        // Get all selected GameObjects in the scene
        var selected = Selection.gameObjects;

        if (selected.Length == 0)
        {
            Debug.LogWarning("No GameObjects selected!");
            return;
        }

        foreach (var go in selected)
        {
            // Destroys the object immediately in the editor
            Object.DestroyImmediate(go);
        }
        Debug.Log($"{selected.Length} GameObjects destroyed immediately.");
    }
}
```
