{"id":7573,"date":"2016-04-14T07:42:09","date_gmt":"2016-04-14T05:42:09","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/"},"modified":"2016-04-14T07:42:09","modified_gmt":"2016-04-14T05:42:09","slug":"java-cannot-delete-file","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/","title":{"rendered":"Java: Cannot delete file?"},"content":{"rendered":"<p>You may once went into the struggle to delete a file in java?<\/p>\n<p>So do I&#8230;<\/p>\n<p><a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo.png\"><img loading=\"lazy\" decoding=\"async\" class=\" size-medium wp-image-8112 aligncenter\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo.png\" alt=\"logo\" width=\"225\" height=\"300\" \/><\/a><\/p>\n<p>I went so mad when I figured out that my files were still present even after a file.delete() or file.deleteOnExit(). It always returned False.<br \/>\nSo what&#8217;s the reason for it not to be deleted? In fact it&#8217;s logical&#8230;<\/p>\n<p>When dealing with files you are probably writing into it. So you open a stream to the file like FileWriter or whatever, and you push and flush data to the file.<br \/>\nWhen finished you have to close your stream like writer.close() BUT it doesn&#8217;t close it as you think. In fact there is still pointers\/references to the stream and the file.<br \/>\nHence when trying to delete the file it refuses to do so because it still have &#8216;stream opened&#8217; on it&#8230;<\/p>\n<p>Thus a solution would be to get rid of these unwanted referencing objects; and that&#8217;s easier than you think, just call a garbage collection.<br \/>\nBecause these objects have been set as &#8216;garbageable&#8217; if you call the GC they will be deleted and you will be able to delete the files properly.<\/p>\n<p>To do so just call System.gc();<br \/>\nBut be careful, it will trigger a full GC and can potentially alter your performances, usually it is not recommended to call it manually inside your code, but if you have several files to delete in a loop, just put then in a List and delete them all in once at the end of your process.<\/p>\n<p>For example:<\/p>\n<pre class=\"brush: java; gutter: true; first-line: 1\">System.gc();\nfor(File f : listToDelete){\n\tf.delete();\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You may once went into the struggle to delete a file in java? So do I&#8230; I went so mad when I figured out that my files were still present even after a file.delete() or file.deleteOnExit(). It always returned False. So what&#8217;s the reason for it not to be deleted? In fact it&#8217;s logical&#8230; When [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":7575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197,368],"tags":[808,809,214],"type_dbi":[],"class_list":["post-7573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-application-integration-middleware","category-development-performance","tag-delete","tag-file","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Java: Cannot delete file? - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java: Cannot delete file?\" \/>\n<meta property=\"og:description\" content=\"You may once went into the struggle to delete a file in java? So do I&#8230; I went so mad when I figured out that my files were still present even after a file.delete() or file.deleteOnExit(). It always returned False. So what&#8217;s the reason for it not to be deleted? In fact it&#8217;s logical&#8230; When [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-14T05:42:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"822\" \/>\n\t<meta property=\"og:image:height\" content=\"1096\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Middleware Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Middleware Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/\"},\"author\":{\"name\":\"Middleware Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\"},\"headline\":\"Java: Cannot delete file?\",\"datePublished\":\"2016-04-14T05:42:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/\"},\"wordCount\":263,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png\",\"keywords\":[\"delete\",\"file\",\"Java\"],\"articleSection\":[\"Application integration &amp; Middleware\",\"Development &amp; Performance\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/\",\"name\":\"Java: Cannot delete file? - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png\",\"datePublished\":\"2016-04-14T05:42:09+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png\",\"contentUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png\",\"width\":822,\"height\":1096},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java: Cannot delete file?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\",\"name\":\"Middleware Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"caption\":\"Middleware Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/middleware-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java: Cannot delete file? - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/","og_locale":"en_US","og_type":"article","og_title":"Java: Cannot delete file?","og_description":"You may once went into the struggle to delete a file in java? So do I&#8230; I went so mad when I figured out that my files were still present even after a file.delete() or file.deleteOnExit(). It always returned False. So what&#8217;s the reason for it not to be deleted? In fact it&#8217;s logical&#8230; When [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/","og_site_name":"dbi Blog","article_published_time":"2016-04-14T05:42:09+00:00","og_image":[{"width":822,"height":1096,"url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png","type":"image\/png"}],"author":"Middleware Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Middleware Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/"},"author":{"name":"Middleware Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"headline":"Java: Cannot delete file?","datePublished":"2016-04-14T05:42:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/"},"wordCount":263,"commentCount":0,"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png","keywords":["delete","file","Java"],"articleSection":["Application integration &amp; Middleware","Development &amp; Performance"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/","url":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/","name":"Java: Cannot delete file? - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png","datePublished":"2016-04-14T05:42:09+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#primaryimage","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/logo-1.png","width":822,"height":1096},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/java-cannot-delete-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java: Cannot delete file?"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1","name":"Middleware Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","caption":"Middleware Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/middleware-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/7573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=7573"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/7573\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media\/7575"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=7573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=7573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=7573"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=7573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}