diff --git a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java index f3e1f2fb24d..401f3f12f4b 100644 --- a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -1002,11 +1002,16 @@ public final class NodeEnvironment implements Closeable { private static void tryWriteTempFile(Path path) throws IOException { if (Files.exists(path)) { Path resolve = path.resolve(".es_temp_file"); + boolean tempFileCreated = false; try { Files.createFile(resolve); - Files.deleteIfExists(resolve); + tempFileCreated = true; } catch (IOException ex) { throw new IOException("failed to write in data directory [" + path + "] write permission is required", ex); + } finally { + if (tempFileCreated) { + Files.deleteIfExists(resolve); + } } } }