Easily delete node_modules or other nested directories

During a normal workday, I am constantly switching between different code repositories, some of which I haven't worked on in several weeks. I oftentimes need to start from a clean slate, meaning installing fresh dependencies and clearing out node_modules and other cached build files.

Since many of our projects are mono repositories, it can sometimes be daunting to ensure I delete each desired directory, some of which are nested.

Enter my deletenestedfolders function, which accepts a directory name as the first and only argument. The script does a find in the current directory, and all nested directories, for the provided folder name. It will then output a list of the directories that will be deleted should you not cancel. The script waits five seconds just in case you ran the command in the wrong directory, or provided an incorrect directory name.

Usage

Copy the function below and save to your .zshrc or .bashrc (etc.) to make it available on your command line.

function deletenestedfolders() {
  echo "-------------------------------------------"
  echo "DELETE NESTED FOLDERS"
  echo "-------------------------------------------"

  if [ $# -eq 0 ]; then
    echo ""
    echo "You must pass a directory name."
    return
  fi

  # Get list of node_modules directories
  DIRECTORY_LIST=$(find . -name "$1" -type d -prune)

  # If no node_modules directories, show message and exit
  if [ "${#DIRECTORY_LIST}" -eq 0 ]; then
    echo ""
    echo "There are no \"$1\" directories to delete."
    return
  fi

  echo ""
  echo "Directories to delete:"
  echo "-------------------------------------------"
  echo "$DIRECTORY_LIST"
  echo "-------------------------------------------"
  echo ""

  echo "Preparing to delete directories..."
  echo "You have 5 seconds to cancel (Ctrl + C)"
  echo ""
  # Sleep to let user cancel
  sleep 5

  echo "Deleting directories... this may take a minute."

  find . -name "$1" -type d -prune -exec rm -rf '{}' +

  echo ""
  echo "Deleted all \"$1\" directories in this "
  echo "directory, and its children."
}

To run the function, simply call it and provide it with a directory name:

$ deletenestedfolders "node_modules"

The script will then:

  1. Find any directories inside your current directory that match your input.
  2. Output a list of all of the directories it found that match.
  3. Wait 5 seconds, allowing you to review the list and cancel if you made a mistake or change your mind.
  4. Delete all found directories, and outputs a message once completed.
$ deletenestedfolders "node_modules"
-------------------------------------------
DELETE NESTED FOLDERS
-------------------------------------------

Directories to delete:
-------------------------------------------
./node_modules
-------------------------------------------

Preparing to delete directories...
You have 5 seconds to cancel (Ctrl + C)

Deleting directories... this may take a minute.

Deleted all "node_modules" directories in this
directory, and its children.