How can I delete a symbolic link?

328

85

I created a symlink using mklink. Now I need to change it but I can't figure out how to delete it so I can recreate it correctly.

Ben V

Posted 2010-07-23T23:22:52.303

Reputation: 3 433

Answers

569

Be very careful.

If you have a symbolic link that is a directory (made with mklink /d) then using del will delete all of the files in the target directory (the directory that the link points to), rather than just the link.

SOLUTION: rmdir on the other hand will only delete the directory link, not what the link points to.

eddyq

Posted 2010-07-23T23:22:52.303

Reputation: 5 724

When I tried rmdir for a sym link I'd created it gave me an error. I could delete the shortcut in Win Explorer though and it didn't affect the target. This was for a link I created using mklink w/o any flags. When I use the \D flag to create the link then I think rmdir might work better (although I didn't try it). – jinglesthula – 2014-07-02T16:10:22.103

8There Microsoft Employees go again, just trying to ruin my life, career, and Windows operating system. – ThorSummoner – 2014-08-25T07:52:36.740

2I was concerned about rmdir on a dir that isn't a SYMLINKD but rmdir will offer a warning if it's a directory that's not empty. – JimFred – 2016-12-20T16:47:08.980

1You can also use rm command in Window bash shell. – Tony OHagan – 2017-01-05T08:39:03.183

2I don't get at which moment the devs considered it logical to delete files behind a link when deleting the link. After the 5th or 6th beer? Or perhaps after the mushrooms... – Romain Vincent – 2017-07-08T22:11:30.230

1@RomainVincent, for backward compatibility reasons, a "directory link" (i.e. SYMLINKD) is actually a directory that's also a link (i.e. reparse point). CMD doesn't special case that it's a link. However, it does prompt with an "Are you sure (Y/N)?" warning that it's about to delete "<link path>\*". I agree that it would be better to special case the behavior for directory links. The API itself special cases them, e.g. RemoveDirectory will remove a directory link even though the target directory isn't empty, but trying the same on the link target directly would fail. – Eryk Sun – 2018-05-04T14:35:41.017

I guess Windows is not good at symbolic links and it's safer not to use them at all there. – Ehsan88 – 2019-04-19T05:17:16.730

@Simon Gillbee Didn't it ask you Y/N? – jw_ – 2020-01-09T14:25:51.620

104Having just accidentally done this (deleted the complete contents of the target folder), this an important tip. – Simon Gillbee – 2011-08-08T21:10:08.773

4This answer is not entirely accurate. Del deletes files, not folders. Therefore, you would not use del to delete the link. – AMissico – 2013-03-12T01:47:12.190

13But don't use rmdir in PowerShell. Wrap it in cmd first. See my answer below... – northben – 2013-05-13T21:25:36.537

4How about if i delete it through windows explorer???? – Cheung – 2013-08-11T16:49:13.507

Now you tell me! Doh! – CramerTV – 2014-05-30T01:25:54.620

65

To remove directory symbolic links created with mklink /d it is safe to delete the symbolic link in windows explorer.

ddelrio1986

Posted 2010-07-23T23:22:52.303

Reputation: 759

3It is correct. I do it all the time. Just tested it again right now. – ddelrio1986 – 2016-09-29T21:00:48.447

2This just worked for me as well. i deleted the link not the source folder that was linked to. perhaps that the catch. – R Hughes – 2017-02-24T15:59:45.097

3It is safe to just delete it with Rightclick -> Delete. This does NOT delete the linked folder. – Hexo – 2017-09-29T13:24:49.293

mklink /d Didn't work for me on Windows 7.. I may have been using an old version though – abelito – 2019-05-17T03:49:49.693

27+1 for telling people it is save to delete symbolic link through Windows Explorer. – AMissico – 2013-03-12T01:45:23.673

49

For a symlink to a file, use del. For a symlink to a directory made with mklink /d, use rmdir.

George V. Reilly

Posted 2010-07-23T23:22:52.303

Reputation: 730

5Warning: "del" will delete the file and not just the link. – WoodenKitty – 2015-11-12T00:39:42.223

6Warning from @Tristan is not correct, this answer is accurate. Tested on windows 7. – jiggunjer – 2016-06-23T03:07:33.340

1In Windows 7 SP1 (64-bit), a symbolic link that points to either a file (e.g. created using MKLINK) or to a directory (MKLINK /D) can be deleted in Windows Explorer, using the ordinary Windows GUI 'delete' option, without deleting the target. Even a JUNCTION (e.g. created using the MKLINK /J command), if deleted in Explorer, will not delete the target directory nor its contents. If not empty, the target directory cannot even be deleted using the command DEL junction at the command prompt. Tested today. – Ed999 – 2017-01-20T15:56:58.080

On Windows Server 2012, this removed the directory contents that my symlink was pointed to... – C Bauer – 2018-06-18T14:12:57.650

You should warn people not to rmdir links from Powershell! – NH. – 2018-10-29T16:57:47.423

29

In Powershell, don't use rmdir! Use cmd /c rmdir .\Target instead. I tested this myself and confirmed it here: http://kristofmattei.be/2012/12/15/powershell-remove-item-and-symbolic-links/

northben

Posted 2010-07-23T23:22:52.303

Reputation: 1 071

That blog confuses the meaning of "Target", which is the actual directory the link points to, and which folks generally wish to avoid deleting whilst removing the link. – kreemoweet – 2015-09-22T16:59:49.853

2rmdir is an internal command of cmd.exe, thus obviously to call it from other shells you'll need cmd /c or cmd /k. rmdir in PowerShell is just an alias to Remove-Item – phuclv – 2019-02-07T00:17:08.637

24

There is another solution tested by me and safe to use. just add to the real folder _ (example: foo become foo_) then simply delete your symbolik link, then remove _ from your true folder.

vcorp

Posted 2010-07-23T23:22:52.303

Reputation: 341

2yeah, this is 100% safest solution after you know that powershell does not give a s**t about rmdir – test30 – 2014-07-03T14:54:52.027

This is a clever precaution. +1 – Hanna – 2015-03-04T17:11:25.727

Warning: I don't think this MIGHT not work on Win10 since it's fixing shortcuts upon renaming. (At least classic Shortcuts) Not tested though. – Hexo – 2017-09-29T13:26:59.210

I did this just in case. After I renamed the target folder, the symbolink link failed when I tried to access it, so I could delete it without worrying. – Andrew – 2018-04-14T00:39:44.050

21

mklink cannot be used to delete symbolic links. To remove a symbolic link, simply delete them as if you’re removing a normal file. For example, to delete the foo symbolic link created above, enter the following command:

If the link is a hard link to a directory:

C:\test>rmdir foo

or else, IF the link points to a file (as opposed to a dir)

C:\test>del foo

Source: http://www.mydigitallife.info/2007/05/22/create-symbolic-links-hard-links-and-directory-junctions-in-vista-with-mklink/

Daisetsu

Posted 2010-07-23T23:22:52.303

Reputation: 5 195

2Hardlinks to directories don't exist. I think you meant to say symlink. – Brilliand – 2015-09-08T18:20:54.073

2Just make sure you don't delete it with del /S or Explorer. – Hello71 – 2010-07-24T00:12:12.453

1See eddyq's answer below because using del for a link made to a directory won't work and instead will attempt to delete teh contents of the folder. – jpierson – 2012-05-07T13:48:55.777

0

In my case (Windows 10), after creating the symbolic link using

MKLINK /D "C:\Users\username\Dropbox\MyProject" "C:\SourceProject"

and deleting via delete using the file explorer or keyboard delete key also deletes the original directory.

What you should do is to remove the link via command prompt.

C:\Users\username\Dropbox>rd /s MyProject

For details about the rd command: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/rd

tetedp

Posted 2010-07-23T23:22:52.303

Reputation: 1

0

The simplest way is to move the folder (at which the symbolic link is pointing) to another place (to parent folder) and then delete the symbolic link. No files will be harmed!

netgoblin

Posted 2010-07-23T23:22:52.303

Reputation: 101

0

I've installed this Link Shell Extension. With this, you have 2 methods.

METHOD 1

Create it using the context menu as shown and deleting via file explorer.

Test with a folder like C:\Windows\Logs as shown or similar. I'm just creating a directory of all the possible locations of temp files to delete and I wanted a central folder where I could monitor the size of them via symbolic links.

Please note: The screen going white is just the UAC.

delete from explorer method

METHOD 2

The safest way undocumented here AFAIK.

On the created symbolic link (in steps shown above), right-click on it this time and click properties then go to the Link Properties tab.

Note: This shell extension added this tab.

Before I go on, lots more info can be found here, also, on the homepage are the x64 and 32bit versions.

So, to continue, here you can add anything to the target field as long as it's different to the original target.


  1. Here's a before with the target as C:\Windows\Logs target before

  2. Here's an after with the target as C:\Windows\Logs_, notice the extra _ at the end.

    target after

  3. Press OK

  4. You can check this again by going over the same 1-3 steps above to ensure that has stuck, this time you will get an error message when you click on the Link Properties tab so the link is now broken.

  5. It is now safe(r) to delete the symbolic link than method 1 IMHO.


But! Both methods work here so it's entirely up to the user's choice. I like the properties method which I happily stumbled upon, so you're 100% sure the link is broken by using step 4 above.

Here's a gif of me creating a symbolic link by right mouse button dragging a folder to another and choosing the option to create a symbolic link in the sub-menu.

To delete, just follow the 1-5 steps above of method 2 or follow method 1.

How to create and delete a symbolic link using the properties method

Please note: The screen going white is just the UAC.

gif on how to create and delete syn links using the properties method

Ste

Posted 2010-07-23T23:22:52.303

Reputation: 361