I have observed that calling DirectoryExists(x) immediately after calling TDirectory.Delete(x) returns true IF the folder to be deleted has few files in it AND the folder is open (in Total Commander).
In other words:
begin
TDirectory.Delete('x', true); <-- 'Delete' exited but the folder is still not fully deleted
if SysUtils.DirectoryExists('x')... <-- This returns true
end;
Is this a normal behavior?
The "solution" is this:
begin
TDirectory.Delete('x', true);
Sleep(1000); //wait for Delete to finish
if SysUtils.DirectoryExists('x')...
end;
Question: How do I know when the Delete is ready (how do I eliminate sleep)?
Note: Total Commander does not block the deletion of the folder (I guess) since the folder is deleted anyway (after a while).
Take a look the msdn page about RemoveDirectory: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365488(v=vs.85).aspx
The Remarks section says:
The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed.
So probably another process also has a handle to the directory (virus scanner?).
If you need to empty the directory, then empty it instead of deleting it and recreating it afterwards. In the end you always pay for a dirty hack ;)