OrchardCMS/OrchardCore · error · FileStoreException

Cannot delete file ' '.

Error message

Cannot delete file '{path}'.

What it means

Catch-all in TryDeleteFileAsync: the mapped File.Delete threw an unexpected exception, rethrown as FileStoreException. Missing files are not an error here (the store returns false); this signals the delete itself failed.

Solutions

  1. Read the InnerException for the underlying IO error.
  2. Ensure no open FileStream/Stream from GetFileStreamAsync is still holding the file; dispose streams before deleting.
  3. Fix permissions on the storage root or retry after the lock is released.

Example fix

// before
using var stream = await store.GetFileStreamAsync(path);
// file still open
await store.TryDeleteFileAsync(path); // fails on Windows
// after
using (var stream = await store.GetFileStreamAsync(path)) { /* read */ }
await store.TryDeleteFileAsync(path);
Defensive patterns

Strategy: try-catch

Validate before calling

var info = await store.GetFileInfoAsync(path);
if (info is null) return; // nothing to delete, TryDeleteFileAsync would return false anyway

Try / catch

try { await store.TryDeleteFileAsync(path); }
catch (FileStoreException ex)
{
    _logger.LogError(ex.InnerException, "Delete failed for {Path}", path);
    throw;
}

Prevention

When it happens

Trigger: TryDeleteFileAsync(path) when the file exists but is locked by another process, the process lacks delete permission, or the path is invalid/read-only.

Common situations: File held open by an upload stream or antivirus scanner on Windows; permission denied on the media folder; network share temporarily unavailable.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/ef807b80610360b6. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs:218

    public Task<bool> TryDeleteFileAsync(string path)
    {
        try
        {
            var physicalPath = GetPhysicalPath(path);

            if (!File.Exists(physicalPath))
            {
                return Task.FromResult(false);
            }

            File.Delete(physicalPath);

            return Task.FromResult(true);
        }
        catch (Exception ex)
        {
            throw new FileStoreException($"Cannot delete file '{path}'.", ex);
        }
    }

    public Task<bool> TryDeleteDirectoryAsync(string path)
    {
        try
        {
            var physicalPath = GetPhysicalPath(path);

            if (!Directory.Exists(physicalPath))
            {
                return Task.FromResult(false);
            }

            Directory.Delete(physicalPath, recursive: true);

            return Task.FromResult(true);
        }

View on GitHub (pinned to 4306c0717f)