[Query]: Why nix-collect does remove packages are already removed

I have installed multiple packages using local nixpkgs repo. The installation works fine but when I have removed the package using nix-env -e, the paths and derivation still exist in the /nix/store. I am able to clean the store by using nix-store --delete /path/to/package/to/be/removed but this is not a clean approach as it leaves all the drv files etc still there. What am I missing here.

Nix is garbage collected. It doesn’t delete anything with most commands. This means that if you ever need the files again, it won’t have to redownload/rebuild them.

If you want to recover disk space, use nix-collect-garbage. Also see the man page for convenient options to delete old profiles (as those are gc roots). For example, uninstalling a package with nix-env will still leave it in the old profile generation, so until you delete that profile generation, the package can’t be garbage-collected.

nix-collect-garbage -d will delete all old versions of your profile, ensuring that everything will be collected that’s not in a live profile. nix-collect-garbage --delete-older-than 7d is what I usually use, which deletes profile generations that are older than 7 days, so that way I can still roll back to recent stuff.

Thanks for the response. Actually I should have stated that earlier. I ran the nix-collect-garbage -d which should ideally remove everything that is not stated in config.nix and home.nix and any other manually installed package via nix-env. However, the nix-collect-garbage -d still leaves several packages that not listed anywhere to be installed. Am I missing something here.

I recommend the nix-du program for visualising what’s going on with your store and your gc roots.