{"id":112506,"date":"2026-07-06T07:00:00","date_gmt":"2026-07-06T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112506"},"modified":"2026-07-04T07:08:33","modified_gmt":"2026-07-04T14:08:33","slug":"20260706-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260706-00\/?p=112506","title":{"rendered":"I opened a file with <CODE>FILE_<WBR>FLAG_<WBR>DELETE_<WBR>ON_<WBR>CLOSE<\/CODE>, but now I changed my mind"},"content":{"rendered":"<p>The <code>CreateFile<\/code> function has a flag called <code>FILE_<wbr \/>FLAG_<wbr \/>DELETE_<wbr \/>ON_<wbr \/>CLOSE<\/code>, which means that the file will be deleted when the last handle to the file is closed. But what if you pass that flag and then change your mind? Is there a way to call take-backs?<\/p>\n<p>No, there are no take-backs. The <code>FILE_<wbr \/>FLAG_<wbr \/>DELETE_<wbr \/>ON_<wbr \/>CLOSE<\/code> flag is permanent.<\/p>\n<p>So what do you do if you want to make a file deleted when the last handle is closed, but only based on some condition determined later?<\/p>\n<p>What you can do is open the file normally, and then once you realize that you want to delete it on last close, you can turn the &#8220;delete on close&#8221; flag on.<\/p>\n<pre>BOOL MarkFileAsDeleteOnClose(HANDLE file)\r\n{\r\n    FILE_DISPOSITION_INFO info{};\r\n    info.DeleteFile = TRUE;\r\n    return SetFileInformationByHandle(hfile,\r\n        FileDispositionInfo, &amp;info, sizeof(info));\r\n<\/pre>\n<p>Unlike <code>FILE_<wbr \/>FLAG_<wbr \/>DELETE_<wbr \/>ON_<wbr \/>CLOSE<\/code>, you can take back the DeleteFile disposition.<\/p>\n<pre>BOOL MarkFileAsNoLongerDeleteOnClose(HANDLE file)\r\n{\r\n    FILE_DISPOSITION_INFO info{};\r\n    info.DeleteFile = FALSE;\r\n    return SetFileInformationByHandle(hfile,\r\n        FileDispositionInfo, &amp;info, sizeof(info));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t change your mind, but you can do it a different way.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-112506","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can&#8217;t change your mind, but you can do it a different way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=112506"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112506\/revisions"}],"predecessor-version":[{"id":112507,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112506\/revisions\/112507"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=112506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}