After executing File.Delete, the file remains in DELETE PENDING

How is it possible that after calling File.Delete file still exists? I used simple code to reproduce the problem using File.Open . The expected exception is a FileNotFoundException . I checked the operation in Process Monitor v3.05, and the result for the file is “DELETE PENDING” and throws a UnauthorizedAccessException . Does anyone have an explanation?

 public class Program { private const string DummyFileName = "dummy.txt"; private static void Main(string[] args) { int attempt = 0; while (true) { using (File.Create(DummyFileName)) { } File.Delete(DummyFileName); try { attempt++; using (File.Open(DummyFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { } } catch (FileNotFoundException) { } catch (UnauthorizedAccessException ex) { Console.WriteLine("File exists{0}", File.Exists(DummyFileName)); Console.WriteLine("File remains in DELETE PENDING state in attempt {0}.", attempt); Console.WriteLine(ex); Console.ReadKey(); } } } } 
+6
source share
1 answer

Windows allows a process to delete a file, even if it is still open by another process (for example, Windows Indexing Service or antivirus). It receives an internal label as "pending removal." The file is not actually deleted from the file system; it is still present after calling File.Delete. Anyone who tries to open the file after this gets an access denied error. The file is not actually deleted until the last handle to the file object is closed.

+2
source

Source: https://habr.com/ru/post/986043/


All Articles