0

If deleting files just removes the references then why not just delete the actual files instead of just removing the references?

Just removing the references leaves the file intact until it is overwritten, but deleting the whole file is better since it makes more space available immediately.

So why are OSes removing the references instead of deleting the entire file?

3
  • 1
    Removing a reference works immediately. The place occupied by the file is declared as free immediately. Wheres as deleting the file contents (if you mean overriding file contents with some new contents like random data stream) can take much time. Depending on file size and device, it can take from some minutes to some hours. Why do you think it would be done "immediately"?
    – mentallurg
    May 26, 2022 at 0:15
  • There's also a correctness argument. On a filesystem that supports hardlinks, there can be two references to the same data. If you delete one and (in your hypothetical system) it zeros out the data, then that other reference to it is now useless. May 26, 2022 at 23:21
  • In addition, traditional hard drives doesn't care if there's data where new data should be written. This changed with SSD's and SMR disks. Rather than modify all our filesystems, we use trim to discard data in the background when the drive is idle.
    – vidarlo
    May 27, 2022 at 19:22

3 Answers 3

2

Removing just the reference is faster. It just marks one field on the file metadata to mark it as unused instead of overwriting the entire file. Imagine how slow would it be to overwrite several GB video files instead of just deleting a few bytes on the file information table.

deleting the whole file is better since it makes more space available immediately.

It's not how it works. As soon as the file is marked as deleted, the entire space used by that file is marked as free and the space is available immediately.

So why are OSes removing the references instead of deleting the entire file?

Performance. In some cases you can force the OS to overwrite the entire file (Linux for example have a shred command for that). It don't guarantee that the entire file will be overwritten (SSD over provisioning can make some sectors of the file still be intact even after overwritting).

1
  • Some systems supply srm to make the shredding of the file automatic in the deletion.
    – doneal24
    May 26, 2022 at 19:53
2

Disks are not divided into files, they are divided into blocks. A physical disk doesn't know what a file is at all.

A filesystem adds structure to the disk, including metadata that groups blocks into files. So you can't delete a file from a disk but you can delete a file from a filesystem. The way a filesystem deletes a file is to remove its metadata and add the blocks that were used for the file back to the free list.

Just to complicate things, unix makes a subtle distinction between a filename and a file. A filename is a reference to an inode. The inode holds the actual metadata for the file and references the blocks. An inode can have multiple filenames (hard links to the inode) as well as processes that are holding the inode open. When you delete a filename, the file is not deleted until all of the hard links to the inode are deleted and all processes have closed the file.

To add a security perspective to this, since deleting a file typically just removes the references to the blocks, the file could technically still be recovered. Practically, once the metadata is gone, it is hard to find the blocks and if you look what is in the free list, no easy way to tell which ones were part of the same file, but it is still possible. Some operating systems support a "secure delete" mode where blocks are overwritten with zeros before the blocks are returned to the free list. (Using TRIM (in secure mode) to tell an SSD to return the block to the disk freelist might also work and some filesystems implement this.)

0

On a hard disk drive, you have a bunch of bits, each of which is either one or zero. The bits are never blank. When we say that some storage is blank, we just mean that the ones and zeros there don't encode any meaningful data. When you write to a file, you're setting each bit to a specific state and overriding the previous state. You don't have to erase data before overwriting it with new data and erasing data doesn't make future writes faster. In fact, when you erase data, you're just overwriting all the bits with zeros or some other meaningless data.

On a solid state drive, you do have to erase the storage before writing new data. A properly-configured OS will send commands to the SSD informing it about storage that is no longer used so that the SSD can erase them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .