Jeffrey
Jeffrey

Reputation: 2149

Quickest way to delete lots of small files with powershell

I have a folder, with 24 sub folders, one for each hour. Each subfolder contains at least 100,000 txt files, each up to 10kb in size.

I am trying to find the quickest way to delete the folder, all sub folders and files.

At the moment I am just using:

Remove-Item -path $Folder.FullName -Recurse -Force

However this seems to be quite time consuming. Whats the quickest way to delete all of these using powershell?

Upvotes: 2

Views: 6514

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

You could use the .NET .Delete() method.

[System.IO.Directory]::Delete($folder.Fullname,$true)

Explanation:

Often times, using the .NET methods are faster for file and folder operations than the seemingly equivalent PowerShell commands. Here we are using the .Delete(String,Boolean) variant. String represents the full path of want you want deleted. Boolean represents the True or False value for recursion ($true being yes to recursion). Without recursion, the directories would need to be empty before they could be deleted.

See Directory.Delete Method for more information.

Upvotes: 2

Related Questions