How are deleted files replaced by new ones?

193 views

Say if the internal storage of your phone were to fill up and you deleted something like a video or game, what is it actually doing to the file in order to take it off the storage and how are new files able to just replace it?

In: 3

8 Answers

Anonymous 0 Comments

Other answers here are pretty good, but I’ll add my two cents in:

At the simplest level, your computer keeps an index of all the files on your drive, a bit like a phone book. It stores where the file starts on the disk and where it ends. When you delete a file, your computer looks up the file in the index and crosses it out. It’s not actually deleted, but in the index, it’s as good as gone.

Then when your computer wants to save another file, it looks up the index, sees that a particular spot is free, and just writes over it, overwriting the old file in the process.

In reality, the index doesn’t store info about a whole file, but rather parts of a file. Say for example you have a file that is five blocks long. You delete it, then you want to save a file that is six blocks long in there, but you can’t do it. So instead you save 5 blocks in there, then 1 block elsewhere, and just mark in the index “file A has 5 blocks here, and 1 block there”.

After doing this for a while, your computer will slow down because what if you have a file that is 100 blocks long, but it’s split up into 37 parts, with some at the start and some at the end of the disk? You’d have to read the drive at one point, move the needle (if using an older mechanical drive), read again, move again, and so on.

So people came up with the idea of defragmentation. When your computer has some down time, it’ll shift parts of files around on disk and update the index, so if you want to read a file that is 100 blocks in size, you can just start at the beginning, read 100 blocks, and there’s your whole file. The computer don’t need to hunt around on the disk to find it, because your computer has done its very best to ensure that files are all stacked neatly in a logical order. It’ll do the same with folders, so instead of a whole file being at point A on the disk, and the next file in the folder being at point Z on the disk, it’ll rearrange the files and the index so that the first file in the folder will be at point A, and the next file in the folder will be at point B, and so on.

You are viewing 1 out of 8 answers, click here to view all answers.