LykosAI/StabilityMatrix · error · IOException

Failed to delete file

Error message

Failed to delete file {filePath.FullPath}

What it means

DeleteVerbose wraps file deletion failures in an IOException naming the file path. Before deleting it attempts to reset attributes to FileAttributes.Normal (to defeat read-only flags), but if filePath.Delete() still throws IOException the error is rethrown with the path in the message. This is the per-file stage of the recursive DeleteVerbose walk.

Solutions

  1. Find and close the process holding the file open, then retry DeleteVerbose
  2. Check disk/AV logs for I/O errors; exclude the directory from antivirus real-time scanning
  3. Verify the file is not read-only and permissions allow deletion
  4. If the reset to FileAttributes.Normal failed, clear attributes manually before deleting

Example fix

// before
filePath.Info.Attributes = FileAttributes.Normal;
filePath.Delete();
// after
var attrs = File.GetAttributes(filePath.FullPath);
File.SetAttributes(filePath.FullPath, attrs & ~(FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System));
filePath.Delete();
Defensive patterns

Strategy: try-catch

Validate before calling

if (!Directory.Exists(dir)) return;
foreach (var f in Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories))
{
    var attrs = File.GetAttributes(f);
    if (attrs.HasFlag(FileAttributes.ReadOnly))
        File.SetAttributes(f, attrs & ~FileAttributes.ReadOnly);
}

Try / catch

try { DirectoryPathExtensions.DeleteVerbose(dir, logger, ct); }
catch (IOException ex)
{
    logger.LogError(ex, "File delete failed under {Dir}; determine locking process", dir);
    // inspect ex.InnerException for Win32 error, retry after closing handles
}

Prevention

When it happens

Trigger: A file inside the directory being deleted is read-only and attribute reset failed, locked by another process, or an I/O error occurs during unlink.

Common situations: Deleting a model/venv directory while the app (or an external editor, indexer, or malware scanner) still has files open; files marked read-only inherited from an archive that lost executable attribute metadata; files on a network share that went offline.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/9278314c5fb0f67b. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Extensions/DirectoryPathExtensions.cs:93

        // Recursively delete all subdirectories
        foreach (var subDir in directory.EnumerateDirectories())
        {
            DeleteVerbose(subDir, logger, cancellationToken);
        }

        // Delete all files in the directory
        foreach (var filePath in directory.EnumerateFiles())
        {
            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                filePath.Info.Attributes = FileAttributes.Normal;
                filePath.Delete();
            }
            catch (IOException ex)
            {
                throw new IOException($"Failed to delete file {filePath.FullPath}", ex);
            }
        }

        // Delete this directory
        try
        {
            directory.Delete(false);
        }
        catch (IOException ex)
        {
            throw new IOException($"Failed to delete directory {directory}", ex);
        }
    }
}

View on GitHub (pinned to af93d6ef57)