stenci
stenci

Reputation: 8481

Deleting environment variables doesn't work

Why deleting an environment variable with reg delete HKCU\Environment /F /V TestVar in Windows 7 Professional removes it from the registry, but the variable still exists?

Here are the details: I created the following 3 .cmd files:

Check variable.cmd

echo TestVar = %TestVar%
pause

Set variable.cmd

setx TestVar 123
pause

Delete variable.cmd

reg delete HKCU\Environment /F /V TestVar
pause

Then I follow these steps (double clicking to make sure that I start a new session every time):

  1. Double click on Check variable.cmd and I see that TestVar does not exist
  2. Double click on Set variable.cmd and it says SUCCESS: Specified value was saved.
  3. Double click on Check variable.cmd and it shows the variable value. Good!
  4. Double click on Delete variable.cmd and it says The operation completed successfully.
  5. Double click on Check variable.cmd and it still shows the variable value. Bad!
  6. Click on the Start menu, type environment, click on Edit environment variables for your account to open the Environment Variables dialog box, click OK without changing anything
  7. Double click on Check variable.cmd and the variable does not exist anymore

I can find the value in the registry after step 2, I cannot find it after step 4, but step 5 still finds it. And even if I don't change anything, step 6 really deletes the variable.

Upvotes: 3

Views: 12298

Answers (5)

Ygautomo
Ygautomo

Reputation: 884

You can use SETX and REG DELETE to delete an environment variable:

ECHO %TestVar%
SETX TestVar "" & REG DELETE HKCU\Environment /v TestVar
ECHO %TestVar%

And then just open a new Command Windows, as noted on the documentation SETX. This also apply when you want to create New Environment variable using SETX. It says "Variables set with setx variables are available in future command windows only, not in the current command window."

Run this command, to ensure the Enviroment Variable is deleted and doesn't exists for the new command prompt session.

SET
ECHO %TestVar%

You could check on Registry Editor, the enviroment variables TestVar already deleted. So I think no need to reboot the Windows.

Upvotes: 0

V SAI MAHIDHAR
V SAI MAHIDHAR

Reputation: 145

DELETING SYSTEM PATH VARIABLE

Method 1:

set var = C:\Users\mahidhai\cygwin64\usr\local\bin
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V var

In my case , it didn't work even though i ran as administrator . Command prompt displays error registry key not found

In such case go directly to registry keys file

Method2:

Go To register editor .
Run -> regedit
NAvigate to 
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Edit directly the register key, by clicking on the register key name .

This should work fine

Method 3:

Sometimes not updated windows could be the issue . Check if any updates  available . If so update immediately

Upvotes: 0

stenci
stenci

Reputation: 8481

Here is the solution to my problem. I don't know if it is the correct solution, but it works for me:

Check variable.cmd

set TestVar
@pause

Set variable.cmd

setx TestVar 123
@pause

Delete variable.cmd

reg delete HKCU\Environment /F /V TestVar
setx DummyVarUsedToDelete ""
reg delete HKCU\Environment /F /V DummyVarUsedToDelete
@pause

setx cannot be used to delete a variable, as explained here, but it does the missing broadcasting after a variable has been removed from the registry with reg delete.

EDIT

I added a line to delete the DummyVarUsedToDelete from the registry. This will not be broadcasted, but it is a small temporary problem.

Upvotes: 5

Harry Johnston
Harry Johnston

Reputation: 36338

You can use setx to delete an environment variable, this will broadcast the correct message:

setx TestVar ""
reg delete HKCU\Environment /F /V TestVar

(As discussed in the comments, using setx by itself will leave an empty entry behind in the registry, which should be deleted for the sake of consistency.)

Upvotes: 1

Trigger
Trigger

Reputation: 79

The registry is read at boot. When a program is started it gets a copy of it's parent's environment, usually Explorer.exe's environment.

As the comment says, explorer (or any other graphical program that acts on the message) if it gets a message from some other program (like setx) warning it that the registry has been changed, rereads the registry.

The answer is simple

Use set and setx on sequential lines.

Upvotes: 0

Related Questions