How to Remove a Symbolic Link (Symlink)
Removing a symbolic link is achieved through the command line, and as we’ll show you, there actually two different ways to undo a soft link. This is aimed at users who spend a lot of time at the command line, but for the less familiar, symbolic links are used in Linux, Mac OS X, and Unix to point one location or file to another location or file, much like how an alias works on Mac OS X Finder or a shortcut works in Windows desktop.
Let’s jump right to it and demonstrate how to delete a symlink.
And yes, this works to delete a symlink in Linux, Mac OS X, or any other modern Unix based operating system.
Remove a Symbolic Link with unlink
The best way to remove a symlink is with the appropriately named “unlink” tool. Using unlink to delete a symlink is extremely simple, you just need to point it at the symbolic link to unlink and remove. As always with the command line, be sure your syntax is precise.
Whether the symbolic link is to a file or a link to a directory, it does not matter, just point directly at the symlink in question and do not append the / trailing slash at the end.
For example, if we were removing a symbolic link from ~/Desktop/hosts to /etc/hosts then you would do the following:
You can always confirm that you’re looking at a symbolic link with the ‘ls -l’ command like so:
-rwxr-xr-x 1 Paul staff 24K Jun 19 11:28 hosts -> /etc/hosts
That will tell you where the symlink is pointing to if you aren’t certain.
The unlink command is basically the rm command, which you can also use if you want to remove a symbolic link.
Delete a Symlink with rm
You can also use the rm command directly to remove symbolic links. If you aren’t really comfortable with using this approach, you can always enable a confirmation with rm and srm commands before they run, which is helpful for novices to the command line or those with notoriously poor syntax accuracy.
Much the same as unlink, be sure you’re pointing at the proper symbolic link and do not include a directory / when specifying the symbolic link to remove, it’s a link and not a real directory after all.
Ultimately it doesn’t matter much which method you choose to remove a symbolic link, just go with what you remember or are comfortable with.
Know of another or better way to modify and delete symbolic links at the command line? Let us know in the comments.
Note that this will not work for directory symlinks (junctions). You will need to use the rmdir command for that. This should be added to this how-to since I’d guess a large majority of the time symlinks will be to directories, not files.
Worked fine for me to remove a symlink to a directory.
unlink worked for me. rm can be dangerous because I’ve deleted the actual contents of a directory using rm -rf trying to remove a symlink tied to a directory.
I’m probably missing something here, but wouldn’t it just be easier to delete a symlink using the Finder?
You can definitely use the Finder to delete symlinks. However, it is nice to know how to do it from the terminal, which is why this article exists.
Why remove the link in the first place when it´s only symbolic?
If you no longer need a symlink, you would remove it. Otherwise the symlink stays in effect.
For example, if you no longer want to be backing up an iPhone to an external hard drive, or linking an iTunes library to another volume.
Kind of obvious for any longtime command line user but if you use ‘unlink’ on a normal file it will delete it, because ‘unlink’ is an alias to ‘rm’
Novices probably should not be in the terminal app anyway.