{"record":{"id":"92bf1742f85a3f36","repo":"OrchardCMS/OrchardCore","slug":"cannot-copy-file-srcpath-because-a-file-already-exists-in-92bf17","errorCode":null,"errorMessage":"Cannot copy file '{srcPath}' because a file already exists in the new path '{dstPath}'.","messagePattern":"Cannot copy file '(.+?)' because a file already exists in the new path '(.+?)'\\.","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStore.cs","lineNumber":598,"sourceCode":"    {\n        try\n        {\n            if (srcPath == dstPath)\n            {\n                throw new ArgumentException($\"The values for {nameof(srcPath)} and {nameof(dstPath)} must not be the same.\");\n            }\n\n            var oldBlob = GetBlobReference(srcPath);\n            var newBlob = GetBlobReference(dstPath);\n\n            if (!await oldBlob.ExistsAsync())\n            {\n                throw new FileStoreException($\"Cannot copy file '{srcPath}' because it does not exist.\");\n            }\n\n            if (await newBlob.ExistsAsync())\n            {\n                throw new FileStoreException($\"Cannot copy file '{srcPath}' because a file already exists in the new path '{dstPath}'.\");\n            }\n\n            await newBlob.StartCopyFromUriAsync(oldBlob.Uri);\n\n            await Task.Delay(250);\n            var properties = await newBlob.GetPropertiesAsync();\n\n            while (properties.Value.CopyStatus == CopyStatus.Pending)\n            {\n                await Task.Delay(250);\n\n                // Need to fetch properties or CopyStatus will never update.\n                properties = await newBlob.GetPropertiesAsync();\n            }\n\n            if (properties.Value.CopyStatus != CopyStatus.Success)\n            {\n                throw new FileStoreException($\"Error while copying file '{srcPath}'; copy operation failed with status {properties.Value.CopyStatus} and description {properties.Value.CopyStatusDescription}.\");","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStore.cs#L580-L616","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Delete the existing destination explicitly first if overwrite is intended.","Generate a unique destination name (timestamp/GUID suffix) when a collision is possible.","Check FileExistsAsync(dstPath) beforehand and branch accordingly.","Clean up duplicates left by failed moves before retrying."],"exampleFix":"// before\nawait _fileStore.CopyFileAsync(srcPath, dstPath);\n// after\nif (await _fileStore.FileExistsAsync(dstPath))\n{\n    await _fileStore.TryDeleteFileAsync(dstPath); // or pick a unique name\n}\nawait _fileStore.CopyFileAsync(srcPath, dstPath);","handlingStrategy":"validation","validationCode":"if (await _fileStore.FileExistsAsync(dstPath))\n{\n    dstPath = MakeUniqueName(dstPath); // e.g. append timestamp/GUID\n}\nawait _fileStore.CopyFileAsync(srcPath, dstPath);","typeGuard":null,"tryCatchPattern":"try\n{\n    await _fileStore.CopyFileAsync(srcPath, dstPath);\n}\ncatch (FileStoreException ex) when (ex.Message.Contains(\"already exists\"))\n{\n    _logger.LogWarning(\"Destination {Dst} exists; choose another name or delete first\", dstPath);\n}","preventionTips":["Check FileExistsAsync on the destination before copying.","Generate collision-proof names (GUID/timestamp suffixes) for uploads.","Make import/migration jobs idempotent: clean or version destinations on re-run.","Remove duplicates left by previously failed moves."],"tags":["azure-blob","file-storage","file-already-exists"],"backgroundTag":"file-already-exists","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}