{"record":{"id":"a7f3671360f48b71","repo":"MuntashirAkon/AppManager","slug":"couldn-t-delete-old-file-inputfile","errorCode":null,"errorMessage":"Couldn't delete old file <inputFile>","messagePattern":"Couldn't delete old file <inputFile>","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/backup/BackupItems.java","lineNumber":303,"sourceCode":"                }\n                List<Path> newFileList = new ArrayList<>();\n                // Get desired extension\n                String ext = CryptoUtils.getExtension(mCryptoMode);\n                // Create necessary files (1-1 correspondence)\n                for (Path inputFile : files) {\n                    Path parent = getBackupPath();\n                    String outputFilename = inputFile.getName() + ext;\n                    Path outputPath = parent.createNewFile(outputFilename, null);\n                    newFileList.add(outputPath);\n                    Log.i(TAG, \"Input: %s\\nOutput: %s\", inputFile, outputPath);\n                }\n                Path[] newFiles = newFileList.toArray(new Path[0]);\n                // Perform actual encryption\n                mCrypto.encrypt(files, newFiles);\n                // Delete unencrypted files\n                for (Path inputFile : files) {\n                    if (!inputFile.delete()) {\n                        throw new IOException(\"Couldn't delete old file \" + inputFile);\n                    }\n                }\n                return newFiles;\n            }\n        }\n\n        @NonNull\n        public Path[] decrypt(@NonNull Path[] files) throws IOException {\n            // Decrypt the files but do NOT delete the originals\n            synchronized (mCryptoGuard) {\n                if (mCrypto == null) {\n                    // No encryption enabled\n                    return files;\n                }\n                List<Path> newFileList = new ArrayList<>();\n                // Get desired extension\n                String ext = CryptoUtils.getExtension(mCryptoMode);\n                // Create necessary files (1-1 correspondence)","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/backup/BackupItems.java#L285-L321","documentation":"BackupItems' encryption step writes encrypted copies of each backup file via mCrypto.encrypt and then deletes the original plaintext files. If any Path.delete() returns false the routine throws rather than leaving plaintext files behind.","triggerScenarios":"Calling encrypt (e.g. through encryptedMetadataFile) on a backup directory where one of the source files cannot be deleted after successful encryption — typically a filesystem/permission problem on the backup storage path.","commonSituations":"Backing up to SD card/SAF-backed storage with restrictive mount flags, files held open by another process, read-only volume, SELinux denials on external storage.","solutions":["Check the backup directory is writable (mount flags, SAF permissions) and retry the backup","Delete the file manually and re-run the backup","Move the backup directory to internal app storage","Ensure no other process (file manager, sync app) holds the files open"],"exampleFix":"// before\nFile backupDir = new File(externalStorage, \"backups\");\n// after\nFile backupDir = new File(context.getFilesDir(), \"backups\"); // always writable\nif (!backupDir.canWrite()) { /* request permissions or pick another dir */ }","handlingStrategy":"try-catch","validationCode":"// before backup:\nif (!backupDir.canWrite() || backupDir.getFreeSpace() < requiredBytes) {\n    // relocate backup storage or request permissions\n}","typeGuard":null,"tryCatchPattern":"try {\n    backup(...);\n} catch (IOException e) {\n    if (e.getMessage().startsWith(\"Couldn't delete old file\")) {\n        // plaintext leftovers remain; clean up and retry\n    }\n}","preventionTips":["Back up to app-internal storage when possible","Avoid holding backup files open in other processes","Check storage permissions before starting backups"],"tags":["android","backup","file-io","encryption"],"backgroundTag":"file-write-failed","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}