How to delete huge directory with millions of files?


I had to delete 8 millions files and folders. Opening up Windows Explorer and hitting delete key would take days to delete the whole directory structure. Killer of this deletion is ENUMERATION. That’s right, it takes forever to enumerate directory structure and file.

I came up with Power Shell command that can delete the whole directory structure quick and painless. Even “Remove-Item” command-let does the same thing as like GUI. So I decided to start deleting the forth-level directories (with files)  and up. The command I used is,

Get-ChildItem | Get-ChildItem | Get-ChildItem | Get-ChildItem | Remove-Item –Force –Recurse

The above piped command took about a day to delete. I was amazed. Use this command on the power shell on the root directory.

Explanation: Get-ChildItem lists the child directory and second Get-ChildItem list the grand-children directories. In the end, we start deleting the from forth-level directories and slowly start deleting to level up.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s