Q100127: How to delete or unset environment variables

Follow

SUMMARY

This article explains how to permanently delete or temporarily unset environment variables for supported versions of Windows, macOS and Linux. Environment variables are dynamic-named values which can be used to alter the behaviour of a process that uses them.


MORE INFORMATION

Deleting environment variables permanently

Deleting environment variables permanently ensures that the variables are removed system wide. Below are instructions for how to permanently delete environment variables set for each platform:

Windows
  1. Type "env" in the Taskbar Search,
    and select Edit the system environment variables.
    Screen_Shot_2021-12-09_at_1.43.35_PM.png
  2. Click the Environment Variables... button.
    Screen_Shot_2021-12-09_at_1.45.30_PM.png
  3. Select the environment variable you want to delete and click Delete.
    Screen_Shot_2021-12-13_at_10.52.46_AM.png
  4. Repeat step 4 as many times as necessary.
  5. Click OK.

NOTE: When deleting either user or system variables, you may need to log off and on again before your changes to environment variables take effect.

macOS

macOS has a few ways that environment variables could be set: launchd.conf, .plist, or shell config file. Make sure to delete the environment variable using all methods you used to create the environment variables and where you’ve stored them.

If you used the launchd.conf file to set environment variables:

  1. Open the Terminal app.
  2. Enter the following command and confirm your password when prompted:
    sudo vi /etc/launchd.conf 
  3. Use the arrow keys to move the cursor to the environment variable you want to delete.
    TIP: Environment variables use either of the following formats:
    setenv <VARIABLE> <VALUE>
    or export VARIABLE = value
  4. Delete the line by pressing the "d" key twice (dd) on the keyboard.
  5. To save and quit, type colon and "x" (:x) then press Enter on the keyboard.
    TIP: If you want to undo, press the escape key, colon and "u" (:u), and Enter.

If you used a .plist file to set environment variables:

  1. Open the ~/Library/LaunchAgents/my.environment.plist file with the editor of your choice, like TextEdit.
    NOTE: "my.environment" may be named differently; please check all files in ~/Library/LaunchAgents if unsure.
    TIP: To navigate to ~/Library/LaunchAgents, open Go to the folder with Command + Shift + G.
  2. Delete the entire line of the environment variable(s) and value(s) in the file. Environment variables will use the following format:
    launchctl setenv MY_VARIABLE my_value
  3. Save.

If you used a shell config file, see the On Linux section below for more information on how to delete them.

Restart your Mac to apply the changes.

Linux

The procedure for deleting an environment variable depends on what your default shell is.

  1. To get the name of the shell you are using, open a Terminal window and enter echo $SHELL.
  2. Depending on the output of the previous step, do one of the following:
  • If your shell is a csh or tcsh, delete any environment variable(s) and value(s) from the .cshrc or .tcshrc files.
    TIP: Environment variables use the following format: setenv VARIABLE value. For example:

    setenv NUKE_PATH /SharedDisk/Nuke

OR

  • If your shell is a bash, ksh, or zsh, delete any environment variable(s) and value(s) from the .bashrc or .kshrc files
    TIP: Environment variables use the following format: export VARIABLE = value. For example:

    setenv NUKE_PATH=/SharedDisk/Nuke

NOTE: If you don’t want to delete your environment variables, comment them out to unset them. For example, if you’re using bash, add # in front of the environment variable.

NOTE: If you have set environment variables in any other files, make sure you delete or unset them as well.

Unsetting environment variables temporarily

Unsetting environment variables temporarily allows you to troubleshoot by changing a particular environment variable just for that Command Prompt/Terminal session. Other sessions on the same system will continue to use the environment variables you unset in that session. Below are instructions for how to temporarily unset environment variables set for each platform:

Windows
  1. Launch Command Prompt

  2. Unset the environment variable by executing the set command using but setting the value to nothing, for example:
    set NUKE_PATH=
  3. Launch your application from the same Command Prompt session for it to not have that environment variable set
macOS
  1. Launch Terminal

  2. Unset the environment variable by executing the export command using but setting the value to nothing, for example:

    unset NUKE_PATH

  3. Launch your application from the same Terminal session for it to not have that environment variable set

Linux

The procedure for unsetting an environment variable  on Linux depends on what your default shell is. 

  1. Launch Terminal
  2. Run the command:  echo $SHELL
  3. a) If your shell is a csh or tcsh shell, then unset the environment variable by executing the setenv command but setting the value to nothing, for example:

    unsetenv NUKE_PATH

    b) If your shell is a bash or ksh shell, then unset the environment variable by executing the setenv command but setting the value to nothing, for example:

    unset NUKE_PATH
  4. Launch your application from the same Terminal session for it to not have that environment variable set



FURTHER READING

More information about handling environment variables can be found here:

Q100015: How to set environment variables

Q100017: How to list environment variables

Q100679: How to get environment variable values

 

    We're sorry to hear that

    Please tell us why