This project is read-only.

Delete Symbolic Link

Sep 11, 2012 at 4:54 PM

Which method should I use in order to delete symbolic links ?

Using Delete(), DeleteFile(), or DeleteDirectory() all result in a Failure ?

I've tried to find on the web if there is something special in order to delete them, but I could not find anything.

Sep 12, 2012 at 10:11 AM

I assume you are using SftpClient.

I need to know what this "Failure" is.

Sep 12, 2012 at 4:25 PM

I confirm this is using SftpClient.

Dec 28, 2012 at 2:21 AM



From protocol point of view I didn't see any command that can be used to delete link only to create one.

What happens when you try to resolve file using link name it will return an actual file,




Dec 28, 2012 at 10:17 AM


So it should normally work like deleting a normal file ?

Because, I still get a failure while trying to remove a symbolic link. And deleting the file doesn't delete the link.

Dec 28, 2012 at 4:23 PM

I actually just tried to delete link, after file it was linked to was deleted and it worked.


I used this code to delete it: sftp.Delete("t1");

Did you use latest source code version?



Dec 31, 2012 at 11:06 AM
Edited Dec 31, 2012 at 11:07 AM


I updated to the lastest source code and still get the problem.

The first time I call sftp.Delete("linkToFile"); it deletes the file linked to the link.
The second time I call sftp.Delete("linkToFile"); in order to delete the link, I receive a No Such File message. (also tried with DeleteFile and DeleteDirectory).

Concerning link to directories, I get a failure when calling: sftp.Delete("linkToDir"); but I suppose this is because I should remove the dir myself ?

Jan 3, 2013 at 10:31 PM


I am wondering if that could be a server issue, may be some server will allow delete of symlink using SFTP and some not.

Did you tried the same behavior on different server? is that the same?

Is it possible for me to logon and look at this problem myself?




Jan 4, 2013 at 3:05 AM

Another thought,

As far as deleting directories,

You need to use DeleteDirectory method since Delete can delete only files, this is due to SFTP specification

As far as why it can give you an error here possible explanations from specification:

An error will be returned if no directory
   with the specified path exists, or if the specified directory is not
   empty, or if the path specified a file system object other than a

So I suspect because this is link and not directory object it fail. So it seems to me it should always fail then.

Jan 4, 2013 at 9:34 AM


So I made some test on different servers, they all result in the same behaviors.

I created a file and a link to it.
I created a directory and a link to it. 

So in my dir I have:

linkToDir -> testDir/
linkToFile -> testFile


I first call DeleteDirectory on linkToDir, it deletes testDir/, then I call DeleteFile on linkToDir, it deletes linkToDir.
I call DeleteFile on linkToFile, it deletes testFile, then I call DeleteFile on linkToFile, and I got a SftpPathNotFoundException.

I confirm that calling DeleteDirectory on linkTofile, result in an error, since there is no such directory, linkToFile is a file, and it is pointing to a file.

So I guess, if I don't know at what the link is pointing to, I first have to call DeleteDirectory, and if it doesn't work call DeleteFile, in order to delete the item at what the link is pointing to.
But as you can see, I cannot remove the link if it was pointing to a file, because DeleteFile (and Delete), throws a SftpPathNotFoundException.

I have tested this with FileZilla, a Delete action on linkToDir, you are proposed to remove the link or the directory pointed at. If you remove the dir, the link is still present, you can then remove it. A Delete action on linkToFile, removes both the link and the file.
I also tried FlashFXP, it also allows me to delete everything, it doesn't follow symlink, so if I remove linkToDir, or linkToFile, I still have to delete testDir/ and testFile myself, but I can delete them all.