9

Is there any way to quickly remove many symbolic links in many folders that contains many other files? E.g some command in command prompt (be able to delete all slink created from day XX for example)?

Remove all objects that are symbolic link, all other objects remain. Now I only navigate folder by folder, manually selecting them in Windows Explorer (rearrange all items by size, and symbolic links size 0KB, and shift key select all --> del) and remove slowly, maybe sometime mistake sl with another 0KB files. My younger bro drag & drop a lot of files with right click (Link Shell Extension Copyright (C) 1999 - 2008 by Hermann Schinagl already installed in PC, integrated into Windows Explorer) and he pick drop as symbolic link, it all became garbage.

0

2 Answers 2

10

This is similar to JdeBP's answer, but it doesn't require any external software:

FOR /F "usebackq delims=" %a IN (`DIR /a:l /s /b "C:\dir\with\symlinks"`) DO RMDIR "%a"

When used in a script, substitute %a with %%a.

3
  • I tried FOR /F "usebackq delims=" %%a IN (DIR /a:l /s /b "D:\") DO RMDIR "%%a". I get %%a was unexpected at this time. Jan 2, 2017 at 16:40
  • When typed directly into the command line, substitute %%a with %a. %%a is for use in a batch file, as it says in the output of for /?. I'll update my answer to include this info.
    – dansmith65
    Jan 3, 2017 at 17:56
  • Note this only works for folder symlinks. For file symlinks change rmdir to del Sep 11, 2019 at 8:37
5

Using the SFUA utility toolkit:

Finding symbolic links can be done the same ways as finding hard links: using the find command that is in Microsoft's SFUA utility toolkit, that runs in the Subsystem for Unix-based Applications:

find . -type l|xargs rm --

Using JP Software's TCC/LE:

This is a simple exercise in the use of attribute switches and the ordinary del command:

del /a:l *
You can, of course, use the /s, /p, and other options to the del command. And you can preview what would be deleted either by using del's /n option or simply by substituting dir for del.

Caveat

Both of these actually find reparse points. Symbolic links are but one form of reparse points. If you have others, such as junctions, you'll have to be careful about what parts of your directory tree you apply these commands to. Using date ranges to only delete files with recent creation dates (/[dc…]) might be profitable as well.

2
  • The TCC/LE version doesn't seem to work with junctions, because /s descends into junctions, rather than considering them to delete. /nj will stop that, but doesn't seem to solve the problem. The following seems to work, even though del claims to have deleted nothing: del /a:l /s /e /l * As a useful helper, this will list all such files: dir /a:l /s /nj /k /m so you can see if it worked. -Robin
    – rlpowell
    Jan 22, 2013 at 8:00
  • This failed to delete the reparse points in my directory tree, but did allow me to find them (substituing dir for del) for deletion via the Windows File Explorer.
    – Zarepheth
    Apr 27, 2017 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .