Delete a file

suggest change

To delete a file (if you have required permissions) is as simple as:

File.Delete(path);

However many things may go wrong:

Note that last point (file does not exist) is usually circumvented with a code snippet like this:

if (File.Exists(path))
    File.Delete(path);

However it’s not an atomic operation and file may be delete by someone else between the call to File.Exists() and before File.Delete(). Right approach to handle I/O operation requires exception handling (assuming an alternative course of actions may be taken when operation fails):

if (File.Exists(path))
{
    try
    {
        File.Delete(path);
    }
    catch (IOException exception)
    {
        if (!File.Exists(path))
            return; // Someone else deleted this file

        // Something went wrong...
    }
    catch (UnauthorizedAccessException exception)
    {
        // I do not have required permissions
    }
}

Note that this I/O errors sometimes are transitory (file in use, for example) and if a network connection is involved then it may automatically recover without any action from our side. It’s then common to retry an I/O operation few times with a small delay between each attempt:

public static void Delete(string path)
{
    if (!File.Exists(path))
        return;

    for (int i=1; ; ++i)
    {
        try
        {
            File.Delete(path);
            return;
        }
        catch (IOException e)
        {
            if (!File.Exists(path))
                return;

            if (i == NumberOfAttempts)
                throw;

            Thread.Sleep(DelayBetweenEachAttempt);
        }

        // You may handle UnauthorizedAccessException but this issue
        // will probably won't be fixed in few seconds...
    }
}

private const int NumberOfAttempts = 3;
private const int DelayBetweenEachAttempt = 1000; // ms

Note: in Windows environment file will not be really deleted when you call this function, if someone else open the file using FileShare.Delete then file can be deleted but it will effectively happen only when owner will close the file.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents