RemoveAll Method

The RemoveAll method removes all key, item pairs from a Dictionary object.

Syntax

object.RemoveAll( )

Arguments

Object: Required. Always the name of a Dictionary object.

Remarks

The following code illustrates use of the RemoveAll method:

Example 1:

Dim d   ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")

' Add some keys and items.
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

' Clear the dictionary
d.RemoveAll
' Output: Dictionary Object is empty.

Example 2:

Function ClearDictionary
    Dim d   ' Create a variable.
    Set d = CreateObject("Scripting.Dictionary")

    ' Add some keys and items.
    d.Add "x", "Xenon"
    d.Add "y", "Yttrium"
    d.Add "z", "Zirconium"

   
    ' Clear the dictionary
    d.RemoveAll

    ' Check if the dictionary is empty
    If d.Count = 0 Then
        ClearDictionary = "Dictionary has been cleared."
    Else
        ClearDictionary = "Dictionary is not empty."
    End If
End Function

' Usage
Dim result
result = ClearDictionary
' Output: Dictionary has been cleared.

Example 3:

Function ManageDictionary
    Dim d, msg   ' Create some variables.
    Set d = CreateObject("Scripting.Dictionary")

    ' Add some keys and items.
    d.Add "m", "Moscow"
    d.Add "n", "New York"
    d.Add "p", "Paris"

    ' Remove a specific key
    d.Remove("n")

    ' Display the items before clearing
    msg = "Items before clearing:"
    For Each Key In d.Keys
        msg = msg & "Key: " & Key & ", Item: " & d.Item(Key)
    Next

    ' Clear the dictionary
    d.RemoveAll

    ' Check if the dictionary is empty
    If d.Count = 0 Then
        msg = msg & "Dictionary has been cleared."
    Else
        msg = msg & "Dictionary is not empty."
    End If

    ManageDictionary = msg
End Function

' Usage
Dim result
result = ManageDictionary
' Output: Items before clearing: Key: m, Item: Moscow Key: p, Item: Paris Dictionary has been cleared.