How to uninstall all npm packages with one command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# Table of Contents

  1. Uninstall all npm packages with one command
  2. Uninstall all npm packages using a command
  3. Uninstall all Global npm packages
  4. Uninstall all Globally installed packages using a command on macOS or Linux
  5. Uninstall all Globally installed packages using a command on Windows

# Uninstall all npm packages with one command

You can uninstall all npm packages by deleting the node_modules folder and the package-lock.json file from your project's root directory.

Removing the node_modules folder and the package-lock.json files uninstalls the local packages from your project.

You can delete your node_modules folder and package-lock.json file by running the following commands on Windows.

shell
# 👇️ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock

If you are on macOS or Linux, run the following commands instead.

shell
# 👇️ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock

You can also clean your npm cache after deleting node_modules and package-lock.json.

shell
npm cache clean --force

If you need to reinstall the dependencies of your project, issue the npm install command.

shell
npm install

The command will install the dependencies and devDependencies packages from your package.json file.

# Uninstall all npm packages using a command

Alternatively, you can use a command to uninstall all local packages.

If you are on Windows, run the following command using Git Bash.

Open your terminal in your project's root directory (where your package.json) file is and run the following command to uninstall all local packages.

shell
npm uninstall `ls -1 node_modules | tr '/\n' ' '`

uninstall all local packages with command

Here is a screenshot of running the command in Git Bash, on Windows.

windows uninstall all local packages

The command will uninstall all local packages and will then remove them from your package.json file.

If you can't get the command to work, it's easier to just delete the node_modules folder and the package-lock.json file as shown in the previous subheading.

# Uninstall all Global npm packages

One way to uninstall all global npm packages is to use the npm ls -g command to print your globally installed packages and uninstall them.

shell
npm ls -g --depth=0

The command lists all global top-level modules.

npm list global top level modules

Note that npm itself is installed as a global package. It is very likely that you don't want to uninstall npm itself because you might not be able to issue npm commands afterward.

Use the npm uninstall command with the -g flag to uninstall the globally installed packages.

shell
npm uninstall -g package1 package2 package3

uninstall global packages

# Uninstall all globally installed packages using a command on macOS or Linux

If you are on macOS or Linux, issue the following command to uninstall all globally installed packages (except for npm).

shell
npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm

uninstall all global packages

Here is a screenshot of running the command on Windows, in Git Bash.

The command uses npm ls -gp --depth=0 to list all global top-level packages.

The awk portion of the command prints all modules other than npm (because we don't want to uninstall the global npm module).

The xargs portion of the command uninstalls the piped from awk packages.

You can issue the npm ls -g command to verify the global packages have been uninstalled.

shell
npm ls -g

# Uninstall all globally installed packages using a command on Windows

If you are on Windows, open PowerShell and issue the following command to uninstall all globally installed packages.

shell
npm -gp ls --depth=0 | ForEach-Object { Get-Item $_ } | Where { $_.Name -ne 'npm' } | ForEach-Object { npm rm -g $_.Name }

uninstall all globally installed packages on windows

You can issue the npm ls -g command to verify all globally installed packages except for npm have been uninstalled.

An alternative approach on Windows is to manually delete the globally installed packages (except for npm) from the C:\Users\<username>\AppData\Roaming\npm directory.

You can open Explorer and enter %appdata%/npm to navigate to the directory.

uninstall all global packages

Simply type %appdata%/npm in Explorer and press Enter.

Make sure to not delete the npm and npx folders.

select packages you want to delete

Select the packages you want to delete and delete them.

You can also delete the packages you want to uninstall from the node_modules directory (except for npm and npx).

delete from node modules folder

Alternatively, you can use the npm ls -g command and uninstall all packages based on its output.

shell
npm ls -g --depth=0

The command lists all global top-level modules.

npm list global top level modules

Note that npm itself is installed as a global package. It is very likely that you don't want to uninstall npm itself because you might not be able to issue npm commands afterward.

Use the npm uninstall command with the -g flag to uninstall the globally installed packages.

shell
npm uninstall -g package1 package2 package3

uninstall global packages

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.