Skip to content

Instantly share code, notes, and snippets.

@raikusy
Created May 16, 2026 20:49
Show Gist options
  • Select an option

  • Save raikusy/29105c4ea8a2560deef886db1ac2c516 to your computer and use it in GitHub Desktop.

Select an option

Save raikusy/29105c4ea8a2560deef886db1ac2c516 to your computer and use it in GitHub Desktop.
Reusable Fish Function (nuke node_modules)

Cleaning up deep, nested directories like node_modules is a perfect use case for modern CLI tools. While rg (ripgrep) is the king of searching inside files, fd is the absolute best tool for finding and acting upon files and directories.

Here is the fastest, most efficient fd one-liner to securely wipe node_modules (or any other target) in your current directory and all subdirectories.

The One-Liner

To immediately find and delete every node_modules folder, run this in your Fish shell:

Code snippet

fd -IH '^node_modules$' -x rm -rf

How it works:

  • fd: Invokes the fd-find tool.

  • -I (--no-ignore): Crucial. By default, fd respects your .gitignore files. Since node_modules is almost always git-ignored, you must use -I so fd actually sees them.

  • -H (--hidden): Ensures it searches inside hidden directories just in case.

  • ^node_modules$: The search pattern. fd uses regex by default. The ^ (start) and $ (end) ensure you only delete folders named exactly "node_modules" and don't accidentally nuke a folder named my_node_modules_backup.

  • -x rm -rf: The execution flag. For every match fd finds, it immediately executes rm -rf against it.

To delete a different file or directory: Simply replace node_modules in the quotes (e.g., fd -IH '^\.DS_Store$' -x rm -rf). Because rm -rf forces deletion on both files and folders, this command works universally.


The Reusable Fish Function

If you do this often, you can create a dedicated Fish function. It includes a safety check to count the matches before it deletes anything.

Run funced nuke in your terminal, paste the following, and then run funcsave nuke:

Code snippet

function nuke -d "Recursively delete a specific file or folder"
    # Take the first argument, default to node_modules if empty
    set target $argv[1]
    if test -z "$target"
        set target "node_modules"
    end

    echo "Scanning for exact matches of '$target' in "(pwd)"..."
    
    # Do a dry run to count matches
    set match_count (fd -IH "^$target\$" | wc -l)
    
    if test $match_count -eq 0
        echo "Nothing to delete! ✨"
        return 0
    end

    echo "Found $match_count match(es). Nuking..."
    
    # Execute the deletion
    fd -IH "^$target\$" -x rm -rf
    
    echo "Cleanup complete! 🧹"
end

How to use the function:

  • nuke ➔ Deletes all node_modules anywhere in the current tree.

  • nuke coverage ➔ Deletes all folders/files named exactly coverage.

  • nuke .eslintcache ➔ Deletes all your ESLint cache files.


A Note on npkill

While fd is the fastest raw CLI method, if you specifically want to free up hard drive space by deleting old node_modules across your entire machine, you can run npx npkill. It provides an interactive, keyboard-driven UI right in your terminal, sorting all your node_modules folders by size and letting you delete them one by one with the Spacebar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment