36

My understanding is that running rm on a file simply unlinks it, marking the space as free in the filesystem. It should then follow that deleting one file always takes roughly the same amount of time (i.e. delete speed is proportional to number of files, not size of files).

So why does deleting a 15 GB file take over a minute with a simple rm file.tar.gz?

voretaq7
  • 79,345
  • 17
  • 128
  • 213
Tom Marthenal
  • 2,106
  • 7
  • 25
  • 37
  • 5
    What filesystem? – Shane Madden Sep 07 '12 at 03:23
  • 8
    On many file systems, each "block" of free space has to be "marked" free. Large files have more blocks. This is not true of all file systems though! – Chris S Sep 07 '12 at 03:25
  • @ShaneMadden good question; ext4 right now, but I've noticed it on other ext#'s as well. – Tom Marthenal Sep 07 '12 at 03:28
  • That's why every file should have it's own virtualized filesystem so the inodes can be blindly wiped super fast! – thinice Sep 07 '12 at 03:56
  • 1
    Which filesystem would perform better at this case? I am right now using a portable drive to move large VM disks from one machine to another and rm taking forever is driving me nuts. – motobói May 06 '14 at 02:30

1 Answers1

43

It takes a constant amount of time to unlink a single block, but files beyond the size of a single block consist of multiple blocks linked together, and the larger the file the larger the quantity of blocks that are linked.

Ignacio Vazquez-Abrams
  • 45,019
  • 5
  • 78
  • 84
  • 15
    *mumble mumble* UFS *mumble mumble* [soft updates](http://en.wikipedia.org/wiki/Soft_updates) *mumble mumble* delete stuff faster *mumble mumble* pancakes! – voretaq7 Sep 07 '12 at 03:45
  • XFS deletes large files very rapidly, but large numbers of small files very slowly. – Andrew Sep 07 '12 at 04:37
  • 5
    One file will always just use one *inode*. It does use multiple data blocks. – Simon Richter Sep 07 '12 at 07:58
  • 2
    One block to rule them all, one block to find them, One block to bring them all and in the darkness bind them. – Rqomey Sep 13 '12 at 20:02
  • For anyone else wondering what this block size is: "Block size is specified at mkfs time and typically is 4KiB." More info at: https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Blocks – Luc Jan 23 '22 at 12:55