Welcome to the fourth part!
Before I continue with this part, I can’t stress enough that you have to stay extremely focused when deleting stuff from the Registry. One small mistake such as a typo and you could end up having to reinstall your entire PC.
So like I said, this part will handle the deletion part. It’s obvious that when you don’t use a program anymore you don’t want to have Registry entries for it.
Proceed to summary of this post
As in the previous parts, we’ll need to import the namespace and declare the oRegKey variable:
'Import correct namespace Imports Microsoft.Win32 'Declare variable within procedure Dim oRegKey As RegistryKey
Now that we have those, we’ll have to define what subkey to work with:
'Define what subkey to work with: oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
From this point on it’s fairly simple. You have 2 ways to go about this:
- Delete the subkey and it’s values
- Go over each value in the subkey and delete them one by one
I’ll handle the easiest and most commonly used way: “Delete the subkey and it’s values”:
*NOTE: If you have another subkey within your primary subkey, then you’ll have to use a slightly different command. I’ll explain this in a bit.
'Delete the entire subkey and it's values oRegKey.DeleteSubKey("MyApp")
If you run that the entire subkey and it’s values will be deleted.
Now let’s say we have our primary subkey called “MyApp” and within that subkey we’ve created another subkey called “Data”.
The subkey will look like this: “HKEY_LOCAL_MACHINE\SOFTWARE\MyApp\Data\…”
If we use the method I just explained, you won’t be able to delete the subkey at all. So we’ll have to invoke a slightly different command:
'Delete the entire subkey and any possible subkeys of our primary key oRegKey.DeleteSubKeyTree("MyApp")
*NOTE: This approach also works if you have a primary subkey with no other subkeys within that subkey!
That’s all there is to it. You’re now able to delete stuff from the Windows Registry 🙂
'Import correct namespace Imports Microsoft.Win32 Public Sub fnDelete() 'Declare variables Dim oRegKey As RegistryKey 'Define subkey to work with oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True) 'Delete our subkey oRegKey.DeleteSubKeyTree("MyApp") 'Close the RegistryKey oRegKey.Close() End Sub