

Ask HN: why is it faster to delete files from a disc than to write them? - rickyconnolly


======
jacquesm
Writing a file means that all the data has to pass trhough the file system,
the io driver, the disk controller interface onto the disk with a certain
maximum write speed. If the file is large then it will take a long time. The
'bookkeeping' to administer where the file goes is a very small portion
compared to the data of the file in such cases.

When you delete the data you only update the bookkeeping, so that's much
quicker.

If you were to erase the file then it would take _just_ as long as writing it
in the first place.

------
fencepost
Inigo Montoya has it: "You keep using that word. I do not think it means what
you think it means."

"Delete" in almost any computer context means "forget" not "erase." "Forget"
the file metadata (generally the name, the link to the beginning of the chain
of blocks on disk, etc.), "forget" the pointer to a block of memory, etc.
Systems specifically designed for security may have additional provisions that
turn those "forget" operations into "overwrite-and-forget," but in general
simple forgetting is preferred for speed.

------
mooism2
Because you're only removing the directory entry. The file contents are still
on disk, it's just there's no easy way to know _where_ on disk.

------
FlyingAvatar
Here's a very simplified example. Note that I am using some made up notation
here: "." represents NULL charcaters, and the numbers would actually be stored
in hexadecimal.

The contents of 48 byte disk with a 7 byte file allocation table (FAT):

    
    
        [................................................]
         FAT----DATA-------------------------------------
    

Write a file named "blah" with contents "abcdefghi":

    
    
        [blah.90abcdefghi................................]
         FAT----DATA-------------------------------------
    

Notice that the FAT area contains the filename, a "9" representing the length
of the file, and a "0" indicating the spot in the data area where the file
begins. In fact, the file system might choose the put the data somewhere else,
and that's absolutely fine. For example:

    
    
        [blah.95.....abcdefghi...........................]
         FAT----DATA-------------------------------------
    

Represents the exact same contents with the file stored at an offset of 5. So
say I want to delete this file now. A typical file system would do something
like this:

    
    
        [.lah.95.....abcdefghi...........................]
         FAT----DATA-------------------------------------
    

It writes a single NULL byte to the file system to mark the filename as empty,
and the file is gone. None of the rest of the data is cleaned up.

