r/AskComputerScience • u/Mister-Reboot • Aug 07 '21
Why does it take longer to delete big files if all we do is unlinking them ?
Hi guys,
It might appear as a stupid question but I haven't found much googling this, just one guy telling it takes lot of time deleting "multiple blocks linked together".
Intuitively, I would have thought that deleting time would be linear with the number of files but I guess what I picture as the address location is way more complex in reality.
Can someone please enlighten me ?
24
u/igglyplop Aug 07 '21
So the way files are stored on disks is by breaking it up into chunks of size n (often close to a power of two, but that's just details here).
So the number of chunks a file is broken into is num=filesize/n. Now for very small files, num can be very small (1 or 2), where in very large files you can be talking about thousands upon thousands of blocks. Furthermore, these blocks may not appear in linear order (they can be scattered around on disk).
So what does the filesystem do with these blocks? Well the filesystem is responsible for keeping track of what blocks belong to each file. Additionally, it's responsible for keeping track of what blocks are available (empty). So when you delete a file, the filesystem is going through all blocks related to that file and marking them as available. Depending on your storage medium (SSD, HDD, God forbid magnetic tape), this can take some time for very large files.
Does this answer your question sufficiently?
5
u/Mister-Reboot Aug 07 '21
Very clear and detailed answer, thank you very much for taking time to explain it simply !
1
u/froggie-style-meme Aug 23 '21
See I don't get why we store files in chunks. Is it just faster?
1
u/igglyplop Aug 23 '21
It's easier to store things. Imagine if you had a bookshelf, and you started buying books bit by bit. Some books are individual, some are a series. Then sometimes you'll sell an arbitrary book or series of books, creating gaps in your bookshelf. If, when you bought another series, you had to keep all the books in the same series back to back and couldn't put them wherever you had free space on the shelf, then you'd have to move books around to make room every time you got another large series, which takes some time. So yes it's faster and easier to manage, and means that on average you can take advantage of more of the disk.
8
u/josh2751 Aug 07 '21
Most of what's been posted here is completely incorrect.
Bottom line, it depends entirely on the type of filesystem.
In general, when you delete a file, it simply removes the entry in the allocation table that describes the file. It doesn't remove any data in the file at all. That should generally be an O(1) operation (and it is -- running rm on a 4GB file and a 400mb file takes the same amount of time, 18ms on my Mac, I just checked). If you have to actually delete all the blocks, or god forbid overwrite all the blocks, then obviously that scales with the size of the file. That isn't normal behavior for most filesystems.
Some filesystems are more complex, and then if they have other logic that they have to do (like chasing blocks all over the filesystem), then that's where your complexity increases.
1
u/Mister-Reboot Aug 07 '21
Thank you for your answer.
You described what was my intuition here:
That should generally be an O(1) operation (and it is -- running rm on a 4GB file and a 400mb file takes the same amount of time, 18ms on my Mac, I just checked).
But the reason I asked my question here is that on most (I think all) of the computer I've used, both macOS and Windows, deleting big files always took way much longer than small ones so I assumed more complex operations were happening. I mainly delete files via the interface though, so maybe doing it via command line (at least the standard rm params) doesn't result in the same deleting behavior than when you do it via the good old right click > delete ? I'd love to check but I don't have any big file on my current computer
1
u/josh2751 Aug 07 '21
It’s certainly possible that when you “delete” via the GUI interface other things take place. In general, I’d expect that it does nothing more than move that file descriptor into a folder called .Trash - but it may not be that way.
2
u/oxamide96 Aug 07 '21
Well it seems not to be O(1) on ext4. What is it doing that it takes longer and why?
1
u/josh2751 Aug 07 '21
Good question. I’m not a file system guru - just what I observed on apfs and what I’ve seen on fat32 and Ntfs over the years.
5
u/atyuttam Aug 07 '21
Basically, the file size indicates the number of chunks. Which also means the number of chunks to delete increases proportional to the file size. That is why it takes longer to delete large files.
1
40
u/chrisgseaton Aug 07 '21 •
The time taken to delete a file is proportional to its size, because a larger file means more chunks to delete. You need to go through and delete all chunks, not just the first chunk.
(Obviously simplified, but that's basically it.)