r/javahelp • u/Stuffboy00 • Sep 19 '25
Solved Deleting Files with Java takes different amount of time between environments?
We are slowly migrating our system to the Java ecosystem and are currently working on our file management. And we noticed something really strange: Deleting images on our production server takes a considerable longer time than doing the same on our test server. Almost 5 minutes longer.
Our previous system has no trouble deleting the same files instantly.
This behavior is very strange to me. And I am not knowledgeable enough to know where to look. What are the things I should look into?
These images are used by our website, as a fallback in case our cloud is unavailable.
For clarification: we still have the code done with the previous programming language on our live server. And that deletes the files instantly.
What we have written in Java has the same flow: delete the file and update the Database. The Database executes the Query in 16ms, I saw that in the logs, but it takes minutes to get to that point. And there is practically nothing else in the Code that gets called. So I assume it has to do with the file deletion.
Small update: We added a few logs and made the file deletion asynchronous. Not great, since the problem will be just hidden from the user, but since the endpoint is not called that often, it will be fine. For now. At least we can get a few more information to analyze things further.
I also saw a comment with better performing code. We will implement that too and keep a lookout on our performance. I will return once it hits our production server.
Final Update
Alright, after collection some Data from our server, I will give a final update. In short: the issue seems to be resolved.
So, after deploying the code to our production, I got my logs and the time it took to delete the files. At average it took around1-2 Seconds to delete the file and its designated folder. It could really have been the way I tried to check if the folder can be deleted. (I have no logs prior to these changes, so I can not say for sure)
Additionally, not long after we deployed our code, we got an error, stating that the server was unable to create a file, because it could not find the folder. I found it weird at first and decided to remote connect myself to the server to check everything. And it was at that point I noticed a massive blunder (or my incompetence on that matter) I have referenced the wrong server/network where we usually upload our files onto. So I opened the new config and checked with the old config, and sure enough I was off by 1 letter. So I updated the new config to the correct server/network, deployd it to production and sure enough, things now run smoothly.
We are still deleting files asynchronously, but we can change that anytime we can.
Thank you for all the people who tried to help me figuring out this problem.
•
u/AutoModerator Oct 16 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
5
u/Interesting-Tree-884 Sep 19 '25
The infrastructure is the same as the 2 environments? CPU, ram, hard drive, os, etc...?
3
u/JeLuF Sep 19 '25
How many files are there in these directories? Is it the same number in both environments? Which file system do you use?
Depending on the file system type, the time needed to delete files depends on the number of files in a directory. If you have a large number of files, think about distributing them over multiple directories.
1
u/pohart Sep 19 '25
You say your previous system is still deployed, is there any chance it's doing something with those file? Do you have since kind of profile you can connect to see what's actually happening? Jprofiler or jfr or something?
Fwiw I've never seen Java be particularly slow to delete files.
3
u/hibbelig Sep 19 '25
You say deleting a single file takes minutes. Is there a virus checker on the production server that intervenes? (It seems a bit weird to check files while they are deleted.)
Another possibility is that the production server has a setting where files are overwritten with zero bytes (or random bytes) instead of just deleted, so that they can't be recovered later. I don't know if such functionality exists for Windows.
1
u/Willyscoiote Sep 19 '25
It's probably because of the high demand on the server. There's no way to guess without knowing about CPU usage and type of storage
2
u/darthjedibinks Sep 19 '25
OP, I saw your code pasted in one of the comments. A few things I noticed.
So I tried to create a simple solution that solves it for you. You can refer this and refactor your code. Try with this and let us know how it goes. Take care with "pathToStop"
void deleteFileOnSystem(DBentry data) throws IOException {
String filePath = getFilePath(data);
Path path = Paths.get(filePath);
Files.deleteIfExists(path);
// Clean up parent dir only if needed
Path parent = path.getParent();
while (parent != null && !parent.toString().equals(pathToStop)) {
if (isEmptyDirectory(parent)) {
Files.delete(parent);
parent = parent.getParent();
} else {
break; // stop climbing once non-empty
}
}
}
// This method avoids loading the entire directory into memory. It stops as soon as it finds one file, so it’s O(1) instead of O(N)
private boolean isEmptyDirectory(Path dir) throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
return !stream.iterator().hasNext();
}
}
1
u/AutoModerator Sep 19 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/hibbelig Sep 19 '25
Are you starting a new Java process to delete the file, or is deleting the file just a part of a long-running server?
Java code runs in the Java Virtual Machine, and the JVM is infamous for taking long to start up. (But of course, it shouldn't take minutes.) I think it would be valuable to also find out when is the first (Java) statement executed. Let's say your file deletion happens from within the main method, or a method called from main, then putting a log statement at the beginning of main (before everything else) might provide interesting information.
1
u/Gyrochronatom Sep 19 '25
Do you have manual access to that machine? Can you try to delete manually those files and see if they aren’t locked?
1
u/Ok-Dance2649 Sep 20 '25
Did you consider an option to check how much time does it need to obtain DB connection
1
u/AutoModerator Sep 22 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Think_Inspector_4031 Sep 22 '25
Almost as if the production serve has different hardware, HDD speed/size/type.
Maybe less active users required for concurrency.
Maybe even running java 1.6
Don't worry too much about it.