How to Delete Soft Links in Linux

The symbolic link, also known as soft link or symlink, in Linux is a special type of file that works as a shortcut to another file.

You can create a soft link using the ln command. But what about deleting them?

There is no special command for removing symbolic links in Linux. You can use the rm command which is also used for deleting files and directories.

rm symbolic_link_name

You may also the unlink command here. Don't go by its name. It's not only for deleting links; it can also delete files.

unlink symbolic_link_name

Let's see it in detail.

All you have to do is to give the name of the path of the link to the command:

rm name_or_path_of_link

Let's see it with an example. Can you identify the soft link in the long list output of ls command?

abhishek@LHB:~/test$ ls -l
total 4708
-rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
-rw-rw-r-- 1 abhishek abhishek     311 Sep 22 12:19 line.txt
lrwxrwxrwx 1 abhishek abhishek      26 Oct 17 11:24 mylink -> ./Documents/sample-mark.md
-rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
-rw-r--r-- 1 abhishek abhishek   12817 Sep 22 12:28 sample.txt

The link is called mylink. You can identify it in the long listing as it starts with the character l (for link) and the name shows the file it points to.

Let me delete the link and verify it:

abhishek@LHB:~/test$ rm mylink 
abhishek@LHB:~/test$ ls -l
total 4708
-rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
-rw-rw-r-- 1 abhishek abhishek     311 Sep 22 12:19 line.txt
-rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
💡
Deleting a soft link does not delete the original file it links to.

You can verify that the original file still exists.

You can remove multiple symlinks at once with the rm command:

rm symlink1 symlink2 symlink3
GridPane
GridPane helps WordPress agencies and developers build their own self managed WordPress hosting business.

Another way to delete soft links is by using the unlink command. It may sound like this command is only for removing links, it can delete files as well.

To remove a link with unlink, use it like this.

unlink name_or_path_of_link

I'll use the same example I used earlier.

The unlink command is pretty limited. You can not delete multiple links at once with it.

You can create soft links to both files and directories. While you have to remove a directory with -r option, the link to a directory doesn't need that.

Use it the same way you used to delete link to a file:

rm name_or_path_to_link_to_dir

Don't use trailing slashes with the link else it will complain.

abhishek@LHB:~/test$ rm link_to_dir/
rm: cannot remove 'link_to_dir/': Is a directory

Here's an example of deleting a soft link to directory.

⚠️
Never force remove a link to a directory with rm -f because it will delete the contents of the directory.
Force deleting the link to the directory deletes the contents of the actual directory

Unlike soft links, a hard link is almost indistinguishable from the original file. You can only notice it with the inode number.

Can you identify the hard link and the file in the output? Pay attention to the inode numbers.

abhishek@LHB:~/test$ ls -li
total 4716
 544057 -rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json
 544884 -rw-rw-r-- 2 abhishek abhishek     311 Sep 22 12:19 hard_link
 544884 -rw-rw-r-- 2 abhishek abhishek     311 Sep 22 12:19 line.txt
1181365 drwxrwxr-x 2 abhishek abhishek    4096 Oct 17 12:33 my_dir
 546419 -rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json

And deleting a hard link is the same as deleting a file.

rm path_or_name_of_hard_link

What about deleting the linked file?

I can't think of a scenario where you would want to delete the original file while removing the soft link automatically.

Well, you can follow a symbolic link to get to the original file and use it to remove the file.

rm "$(readlink '/path/to/link')" /path/to/link 
💡
If you delete a file, keeping the soft link intact, the link becomes a broken or dangling link.

Conclusion

While the unlink command exists, I recommend using the rm command for deleting symbolic links. You are already familiar with it and use this command for removing files. Use it for links as well. It's one less command to remember.