This documentation is archived and is not being maintained.

RegistryKey.DeleteSubKeyTree Method

Deletes a subkey and any child subkeys recursively. The string subKey is not case-sensitive.

[Visual Basic]
Public Sub DeleteSubKeyTree( _
   ByVal subkey As String _
)
[C#]
public void DeleteSubKeyTree(
 string subkey
);
[C++]
public: void DeleteSubKeyTree(
 String* subkey
);
[JScript]
public function DeleteSubKeyTree(
   subkey : String
);

Parameters

subkey
Subkey to delete.

Exceptions

Exception Type Condition
ArgumentNullException The subkey parameter is a null reference (Nothing in Visual Basic).
ArgumentException Deletion of a root hive is attempted.

The subkey parameter does not match a valid registry subkey.

SecurityException The user does not have RegistryPermission.SetInclude(delete, currentKey) access.

Remarks

You must have appropriate permissions to delete the subkey, and its tree.

CAUTION   Deleting a particular key will remove all entries below the key in the tree. No warning will be provided. If you want to delete a subkey only when it has no child subkeys, use DeleteSubKey instead.

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the RegistryKey class.

[Visual Basic] 
' Delete or close the new subkey.
Console.Write(vbCrLf & "Delete newly created " & _
    "registry key? (Y/N) ")
If Char.ToUpper(Convert.ToChar(Console.Read())) = "Y"C Then
    Registry.CurrentUser.DeleteSubKeyTree("Test9999")
    Console.WriteLine(vbCrLf & "Registry key {0} deleted.", _
        test9999.Name)
Else
    Console.WriteLine(vbCrLf & "Registry key {0} closed.", _
        test9999.ToString())
    test9999.Close()
End If

[C#] 
// Delete or close the new subkey.
Console.Write("\nDelete newly created registry key? (Y/N) ");
if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
{
    Registry.CurrentUser.DeleteSubKeyTree("Test9999");
    Console.WriteLine("\nRegistry key {0} deleted.", 
        test9999.Name);
}
else
{
    Console.WriteLine("\nRegistry key {0} closed.", 
        test9999.ToString());
    test9999.Close();
}

[C++] 
// Delete or close the new subkey.
Console::Write(S"\nDelete newly created registry key? (Y/N) ");
if(Char::ToUpper(Convert::ToChar(Console::Read())) == 'Y')
{
    Registry::CurrentUser->DeleteSubKeyTree(S"Test9999");
    Console::WriteLine(S"\nRegistry key {0} deleted.", 
        test9999->Name);
}
else
{
    Console::WriteLine(S"\nRegistry key {0} closed.", 
        test9999->ToString());
    test9999->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

RegistryKey Class | RegistryKey Members | Microsoft.Win32 Namespace | DeleteSubKey | CreateSubKey | DeleteValue | GetSubKeyNames | SubKeyCount

Show: