Deleting files in optimal way . I wondered about this after deleting some of the files on my computer :

Suppose you have $n^2$ files in a folder , arranged in an $n$ by $n$ square out of which only $n$ are useful and the rest $n^2-n$ are junk . You want to delete the $n^2-n$ junk files while keeping the other $n$ .The $n$ useful files are exactly on the principal diagonal (which connects top-left with bottom-right) . Also (as you may know ) you can select a rectangular block of files by holding the left-click and then deleting them together.
The question is :
What is the most optimal way to delete the junk files while keeping the useful ones .

Note :

*

*When you delete some items in a folder the ones that remain will be reorganized (they don't remain in their position )


*You cannot touch in any way the useful files (ex : move them to other folders ) . You must only delete the junk files .
I found only the obvious way :
Start from the bottom to the top :

*

*On the last row delete the block $ 1 \times (n-1) $

*On the second to last row delete the blocks $1 \times (n-2)$ and $1 \times 1$
and so on ...
In this way I make $2n-2$ deletes but I think we can do significantly better .
Thanks for your help with this question .
 A: I think that you can do it in n deletes.  Delete the first column (below the useful file).  Note that the useful files in 2nd - nth rows now are one element closer to the left.  Delete the first column below the (now two!) useful files.  Continue this a total of n-1 times, at which point all useful files are along the left-most column and simply delete all other columns.  The pictures shows the files (red are useful and green are deleted in each of 4 steps).

if the shift is column first vs row first, then only 2 steps

A: Turns out the problem is (a lot) easier than I thought .
Attach numbers to the positions : from $1$ to $n^2$ from left to right .
On the first column delete all the files except the first , which is useful (namely the files $n+1$ , $2n+1 , \ldots ,n^2-n+1$ )
Now take the useful file on the row $k$ namely $n(k-1)+k$ .
Exactly $k-1$ files will be deleted that are now on smaller positions than this file so after the operation this file will be on the position :
$$n(k-1)+k-(k-1)=n(k-1)+1$$ 
This means that all files will be on the first column and then we only need to delete the remaining rectangle and we're done .
