OrchardCMS/OrchardCore · error · FileStoreException

Cannot copy file ' ' because a file already exists in the…

Error message

Cannot copy file '{srcPath}' because a file already exists in the new path '{dstPath}'.

What it means

CopyFileAsync refuses to overwrite: if a blob already exists at the destination, it throws FileStoreException naming both source and destination. This protects callers from silently clobbering existing files in Azure Blob Storage.

Solutions

  1. Delete the existing destination explicitly first if overwrite is intended.
  2. Generate a unique destination name (timestamp/GUID suffix) when a collision is possible.
  3. Check FileExistsAsync(dstPath) beforehand and branch accordingly.
  4. Clean up duplicates left by failed moves before retrying.

Example fix

// before
await _fileStore.CopyFileAsync(srcPath, dstPath);
// after
if (await _fileStore.FileExistsAsync(dstPath))
{
    await _fileStore.TryDeleteFileAsync(dstPath); // or pick a unique name
}
await _fileStore.CopyFileAsync(srcPath, dstPath);
Defensive patterns

Strategy: validation

Validate before calling

if (await _fileStore.FileExistsAsync(dstPath))
{
    dstPath = MakeUniqueName(dstPath); // e.g. append timestamp/GUID
}
await _fileStore.CopyFileAsync(srcPath, dstPath);

Try / catch

try
{
    await _fileStore.CopyFileAsync(srcPath, dstPath);
}
catch (FileStoreException ex) when (ex.Message.Contains("already exists"))
{
    _logger.LogWarning("Destination {Dst} exists; choose another name or delete first", dstPath);
}

Prevention

When it happens

Trigger: Calling IFileStore.CopyFileAsync(srcPath, dstPath) when a blob already exists at dstPath; also via MoveFileAsync when the target already exists (e.g., retrying a previously half-completed move).

Common situations: Duplicate media uploads with the same filename; re-running a migration/import job without cleanup; concurrent workers choosing the same destination name; retry after error 175 left a partial copy.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStore.cs:598

    {
        try
        {
            if (srcPath == dstPath)
            {
                throw new ArgumentException($"The values for {nameof(srcPath)} and {nameof(dstPath)} must not be the same.");
            }

            var oldBlob = GetBlobReference(srcPath);
            var newBlob = GetBlobReference(dstPath);

            if (!await oldBlob.ExistsAsync())
            {
                throw new FileStoreException($"Cannot copy file '{srcPath}' because it does not exist.");
            }

            if (await newBlob.ExistsAsync())
            {
                throw new FileStoreException($"Cannot copy file '{srcPath}' because a file already exists in the new path '{dstPath}'.");
            }

            await newBlob.StartCopyFromUriAsync(oldBlob.Uri);

            await Task.Delay(250);
            var properties = await newBlob.GetPropertiesAsync();

            while (properties.Value.CopyStatus == CopyStatus.Pending)
            {
                await Task.Delay(250);

                // Need to fetch properties or CopyStatus will never update.
                properties = await newBlob.GetPropertiesAsync();
            }

            if (properties.Value.CopyStatus != CopyStatus.Success)
            {
                throw new FileStoreException($"Error while copying file '{srcPath}'; copy operation failed with status {properties.Value.CopyStatus} and description {properties.Value.CopyStatusDescription}.");

View on GitHub (pinned to 4306c0717f)