# ClearFlags

> Clears the specified flags on all hierarchy nodes.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Hierarchy](/engine/6000.7/script-reference/unity/hierarchy.md)
* **Assembly:** UnityEngine.HierarchyCoreModule

## ClearFlags(HierarchyNodeFlags)

Clears the specified flags on all hierarchy nodes.

```csharp
public void ClearFlags(HierarchyNodeFlags flags)
```

### Parameters

**** (\[HierarchyNodeFlags]\(/engine/6000.7/script-reference/unity/hierarchy/hierarchynodeflags)): The flags to clear on all hierarchy nodes in the view model.

### Remarks

The following example creates a context menu item that collapses all nodes in the Hierarchy window except the paths to the selected items. The action appears in the **Hierarchy Samples** submenu of the context menu. It uses `ClearFlags` to perform the initial collapse.

### Examples

```csharp
using System;
using Unity.Hierarchy;
using Unity.Hierarchy.Editor;
using UnityEditor;
using UnityEngine.UIElements;

namespace Unity.HierarchySamples.Editor
{
    class CollapseOthers
    {
        [InitializeOnLoadMethod]
        static void Initialize()
        {
            HierarchyWindow.PopulateContextMenu += OnPopulateContextMenu;
        }

        static void OnPopulateContextMenu(HierarchyWindow window, HierarchyView view, HierarchyViewItem item, DropdownMenu menu)
        {
            menu.AppendAction("Hierarchy Samples/Collapse Others", a =>
            {
                // Capture selected and expanded nodes.
                int selectedAndExpandedCount = view.ViewModel.HasFlagsCount(HierarchyNodeFlags.Selected | HierarchyNodeFlags.Expanded);
                Span<HierarchyNode> expandedAndSelectedNodes = selectedAndExpandedCount < 16 ? stackalloc HierarchyNode[selectedAndExpandedCount] : new HierarchyNode[selectedAndExpandedCount];
                view.ViewModel.GetNodesWithFlags(HierarchyNodeFlags.Selected | HierarchyNodeFlags.Expanded, expandedAndSelectedNodes);

                // Create a flags change scope. This is an optimization to avoid triggering an update if no flags
                // actually changed after the operation.
                using (_ = new HierarchyViewModelFlagsChangeScope(view.ViewModel))
                {
                    // Collapse all nodes.
                    view.ViewModel.ClearFlags(HierarchyNodeFlags.Expanded);

                    // Expand parents of all selected nodes.
                    foreach (ref readonly var node in view.ViewModel.EnumerateNodesWithFlags(HierarchyNodeFlags.Selected))
                    {
                        var parent = view.ViewModel.GetParent(node);
                        if (parent == HierarchyNode.Null || parent == view.Source.Root) continue;
                        view.ViewModel.SetFlagsRecursive(parent, HierarchyNodeFlags.Expanded, HierarchyTraversalDirection.Parents);
                    }

                    // Expand all nodes that were previously expanded and selected.
                    view.Expand(expandedAndSelectedNodes);
                }

            }, _ => item == null || (view.ViewModel.HasFlagsCount(HierarchyNodeFlags.Selected) == 0)
                ? DropdownMenuAction.Status.Disabled
                : DropdownMenuAction.Status.Normal);
        }
    }
}
```

## ClearFlags(HierarchyNode, HierarchyNodeFlags)

Clears the specified flags on the hierarchy node.

```csharp
public void ClearFlags(in HierarchyNode node, HierarchyNodeFlags flags)
```

### Parameters

**** (\[HierarchyNode]\(/engine/6000.7/script-reference/unity/hierarchy/hierarchynode)): The hierarchy node to clear the flags on.**** (\[HierarchyNodeFlags]\(/engine/6000.7/script-reference/unity/hierarchy/hierarchynodeflags)): The flags to clear on the hierarchy node.

## ClearFlags(ReadOnlySpan\<HierarchyNode>, HierarchyNodeFlags)

Clears the specified flags on the hierarchy nodes.

```csharp
public int ClearFlags(ReadOnlySpan<HierarchyNode> nodes, HierarchyNodeFlags flags)
```

### Parameters

**** (\[ReadOnlySpan\<HierarchyNode>]\(https\://learn.microsoft.com/dotnet/api/system.readonlyspan-1)): The hierarchy nodes to clear the flags on.**** (\[HierarchyNodeFlags]\(/engine/6000.7/script-reference/unity/hierarchy/hierarchynodeflags)): The flags to clear on the specified hierarchy nodes.

### Returns

| Type                                                       | Description                                       |
| ---------------------------------------------------------- | ------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The number of nodes that had their flags cleared. |

### Remarks

Null or invalid nodes are ignored.

## ClearFlags(ReadOnlySpan\<int>, HierarchyNodeFlags)

Clears the specified flags on the hierarchy node indices.

```csharp
public int ClearFlags(ReadOnlySpan<int> indices, HierarchyNodeFlags flags)
```

### Parameters

**** (\[ReadOnlySpan\<int>]\(https\://learn.microsoft.com/dotnet/api/system.readonlyspan-1)): The hierarchy node indices to clear the flags on.**** (\[HierarchyNodeFlags]\(/engine/6000.7/script-reference/unity/hierarchy/hierarchynodeflags)): The flags to clear on the hierarchy nodes at the specified indices.

### Returns

| Type                                                       | Description                                       |
| ---------------------------------------------------------- | ------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The number of nodes that had their flags cleared. |

### Remarks

Invalid node indices are ignored.
