I am removing files in a loop with File.Delete(String) in a simplest .Net Core Console application:
1. foreach (var listing in listings)
2. File.Delete(listing.Path);
BUT the issue is that the application randomly hangs on line 2, no exception is thrown. After application restart, several files are removed successfully and the app randomly hangs, again, on another file.
There are some items to note:
listing.Pathis an absolute path to a file on a disklisting.Pathlooks likeD:\Output\00000001\6b1c8e6c-7d83-481a-a7db-aac9024059c4.png- the length of
listingsarray is about 10 items - file DOES exist in every case
- Storage: Seagate Exos 7E8 512E 8TB 7200rpm 256MB ST8000NM000A 3.5" SATA III HDD
- OS: Windows 10 64x
- total size of
Outputfolder is 210GB Outputfolder contains 137 000 other folders with 928 000 files in total (approximately 5 - 10 files per folder)- average file size is about 350KB
I suspect that issue is somehow related to the amount of files. But File.Delete should be the most reliable way to remove a file, and it just hangs without any response for multiple hours. Which forces me to kill the process of the app. Is there a better or more efficient way to remove a file?
Update: After moving all the files to another Windows machine in a network with an SSD installed and executing the application there it worked perfectly fine. So it may look like a hardware issue. Therefore I will rephrase the question title from How to properly delete a file in .Net Core with C# to How to properly delete a file in .Net Core with C# from a folder with large amount of other files or folders