How to mass remove symbolic links in Windows (among regular files that share similar name in many folders)?

8

4

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.

Edward

Posted 2011-12-24T02:38:51.323

Reputation: 251

Answers

8

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.

dansmith65

Posted 2011-12-24T02:38:51.323

Reputation: 81

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. – Syaiful Nizam Yahya – 2017-01-02T16:40:00.103

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 – 2017-01-03T17:56:49.467

Note this only works for folder symlinks. For file symlinks change rmdir to del – user5389726598465 – 2019-09-11T08:37:06.080

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.

JdeBP

Posted 2011-12-24T02:38:51.323

Reputation: 23 855

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 – 2017-04-27T15:22:18.733

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 – 2013-01-22T08:00:39.950