<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to properly call QFile::remove() on Windows?]]></title><description><![CDATA[<p dir="auto">I am trying to debug my application in Qt Creator using SQLite3 database files which the user can create, open, and overwrite when a new database is created... At least on Linux, I can always overwrite the old database if it is owned by the current user.</p>
<p dir="auto">On Windows, I am getting an error stating that the process cannot access the file because it is in use by another process. If I look at the file in Windows Explorer at the moment that happens, it says that it is in use by my application. So I inserted this code in the hopes that it would solve the error:</p>
<pre><code>    QFile x(absFilePath);

#ifdef Q_OS_WINDOWS
    if (x.isOpen()) {
      x.close();
    }
#endif

    if (!x.remove()) {
      const QString title = tr("Could Not Remove Existing File");
      const QString msg = tr("The file:\n\n%1\n\ncould not be removed.\n\nError message:\n%2");
      QMessageBox::warning(this,
                           title,
                           msg.arg(absFilePath).arg(x.errorString()));
      return;
    }
</code></pre>
<p dir="auto">However, this doesn't let me remove the file. The file path in <code>absFilePath</code> is valid, the file exists, and permissions are adequate to allow deletion of the file. This is all checked in code that runs just before this extract is executed. And any objects such as QFile or QFileInfo which might refer to this file are put in their own scope so that when the above bit runs, they all go out of scope.</p>
<p dir="auto">I can call the static version of <code>QFile::remove(absFilePath)</code> which DOES seem to work, but then I won't be able to get any error messages from QFileDevice when it doesn't work. Is there a better way to do this but still get an error message when it doesn't work?</p>
]]></description><link>https://forum.qt.io/topic/164343/how-to-properly-call-qfile-remove-on-windows</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 06:53:51 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164343.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 27 Feb 2026 13:15:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 15:13:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> said in <a href="/post/836669">How to properly call QFile::remove() on Windows?</a>:<br />
Small correction:</p>
<blockquote>
<p dir="auto">if (us &lt; max_wait) {<br />
// Success, but we waited at least once:<br />
qDebug() &lt;&lt; "Waited for QFile::remove() in microseconds: " &lt;&lt; us.count(); // &lt;== need to use ".count()" here...<br />
} else {<br />
// etc.</p>
</blockquote>
]]></description><link>https://forum.qt.io/post/836675</link><guid isPermaLink="true">https://forum.qt.io/post/836675</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Sat, 28 Feb 2026 15:13:28 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 13:22:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Here you can read the Qt6 source code of <code>QFile::remove()</code> ... the first link is to the non-static member function, and the second is to the static member function. My code does pretty much the same thing except for the <code>"while {...}"</code> loop I have written:</p>
<p dir="auto"><a href="https://codebrowser.dev/qt6/qtbase/src/corelib/io/qfile.cpp.html#421" target="_blank" rel="noopener noreferrer nofollow ugc">https://codebrowser.dev/qt6/qtbase/src/corelib/io/qfile.cpp.html#421</a><br />
<a href="https://codebrowser.dev/qt6/qtbase/src/corelib/io/qfile.cpp.html#452" target="_blank" rel="noopener noreferrer nofollow ugc">https://codebrowser.dev/qt6/qtbase/src/corelib/io/qfile.cpp.html#452</a></p>
<p dir="auto">Does this help?</p>
<p dir="auto">And since there is basically no difference in the actual code, as I said ... I do want to get the error message.</p>
]]></description><link>https://forum.qt.io/post/836673</link><guid isPermaLink="true">https://forum.qt.io/post/836673</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Sat, 28 Feb 2026 13:22:10 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 12:24:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> said in <a href="/post/836668">How to properly call QFile::remove() on Windows?</a>:</p>
<blockquote>
<p dir="auto">there is nothing between the QFile x(...) statement and the #ifdef....</p>
</blockquote>
<blockquote>
<pre><code> QFile x(absFilePath);
 x.close();
</code></pre>
</blockquote>
<p dir="auto">Then I have no idea why you are creating any <code>QFile</code> instance here at all.  And if for unknown reason it is causing problem just remove it, why have you put this in?  Oh, did you say purely in order to be able to go <code>x.remove()</code> so that you can get <code>QFile::error()</code> information for message?  Certainly you should not need the <code>close()</code>, and I asked earlier if your <code>if (x.isOpen())</code> was ever true, because with this code it should never be?</p>
<p dir="auto">Looks like we shall never know why this fails when the static call succeeds....</p>
]]></description><link>https://forum.qt.io/post/836670</link><guid isPermaLink="true">https://forum.qt.io/post/836670</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 28 Feb 2026 12:24:33 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 10:55:50 GMT]]></title><description><![CDATA[<p dir="auto">Maybe this is overkill for something which should be very simple, but at least it is working.<br />
The code right before the following performs some checks on exisence and file permissions. If the user chooses to delete the file, then this part runs:</p>
<pre><code>  QString errstring;

  if (delete_existing_file) {
    std::chrono::microseconds us(0);
    const std::chrono::microseconds us_begin(0);
    const std::chrono::microseconds interval(500);
    const std::chrono::microseconds max_wait(1000000);
    { // start a new scope...
      QFile x(absFilePath);
      x.close();
      // The following snippet taken from the example for std::this_thread::yield():
      // https://en.cppreference.com/w/cpp/thread/yield.html
      auto wait_for_remove = [](std::chrono::microseconds s) {
        auto begin = std::chrono::high_resolution_clock::now();
        auto end = begin + s;
        do {
          std::this_thread::yield();
        } while(std::chrono::high_resolution_clock::now() &lt; end);
      };

      while (!x.remove()) {
        us += interval;
        wait_for_remove(interval);
        if (us &gt;= max_wait)
          break;
      }
      errstring = x.errorString();
    } // x goes out of scope here...

    if (us &gt; us_begin) {
      if (us &lt; max_wait) {
        // Success, but we waited at least once:
        qDebug() &lt;&lt; "Waited for QFile::remove() in microseconds: " &lt;&lt; us;
      } else {
        title = tr("Problem Removing Existing File");
        msg = tr("The file '%1' could not be removed.\n\nError message:\n%2")
                .arg(absFilePath).arg(errstring);
        QMessageBox::warning(this,title,msg);
        qDebug() &lt;&lt; "Failed to remove file: " &lt;&lt; absFilePath &lt;&lt; "Error: " &lt;&lt; errstring;
        return;
      }
    }
  }
</code></pre>
]]></description><link>https://forum.qt.io/post/836669</link><guid isPermaLink="true">https://forum.qt.io/post/836669</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Sat, 28 Feb 2026 10:55:50 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 09:55:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/836667">How to properly call QFile::remove() on Windows?</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a><br />
to be 100% clear, the <code>absFilePath</code> is the path to the actual database file on disk opened by <code>QSqlDatabase</code>, right?</p>
</blockquote>
<p dir="auto">No ... <code>absFilePath</code> is the name of the file chosen by the user which will be the name of the new database. It just so happens that this is an existing file in this part of the code, which happens to be a database. There is no <strong>open</strong> database connection when this code runs, and there is nothing between the <code>QFile x(...)</code> statement and the <code>#ifdef...</code>. The application ensures that any open database is closed (i.e. the connection is removed) before letting the user create a new one.</p>
<p dir="auto">Like <a class="plugin-mentions-user plugin-mentions-a" href="/user/igkh">@<bdi>IgKh</bdi></a> said, it is pretty much what is in the Qt source code for <code>QFile::remove()</code>  ... it also closes the file before trying to remove it. And if it isn't open, then I assume there is no need to call <code>close()</code>. But maybe I should not trust <code>QFile::isOpen()</code> and just call <code>close()</code> in any case?</p>
]]></description><link>https://forum.qt.io/post/836668</link><guid isPermaLink="true">https://forum.qt.io/post/836668</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Sat, 28 Feb 2026 09:55:10 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 09:23:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a><br />
In your sample code you just show:</p>
<pre><code>    QFile x(absFilePath);

#ifdef Q_OS_WINDOWS
    if (x.isOpen()) {
      x.close();
    }
#endif

    if (!x.remove()) {
</code></pre>
<ul>
<li>Put in a debug, do you actually hit that <code>x.close()</code>?</li>
<li>Please show what you actually do with the <code>QFile x</code> in between creating the variable and the close for the attempted remove.</li>
</ul>
<p dir="auto">Oh, and to be 100% clear, the <code>absFilePath</code> is the path to the actual database file on disk opened by <code>QSqlDatabase</code>, right?</p>
]]></description><link>https://forum.qt.io/post/836667</link><guid isPermaLink="true">https://forum.qt.io/post/836667</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 28 Feb 2026 09:23:47 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 08:45:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> and <a class="plugin-mentions-user plugin-mentions-a" href="/user/andy314">@<bdi>Andy314</bdi></a> : Thanks for the feedback. I am pretty sure that the database connection is being handled properly. It is possible that it has to do with my environment ... running in Oracle VirtualBox in a Windows 10 guest on a Linux host. I had an issue once where I could not remove a USB stick in the virtual environment for several minutes because Windows thought it was being used, where it certainly wasn't. I don't have any anti-virus software running in the guest, but maybe it was just Windows doing its housekeeping?</p>
<p dir="auto">I need to look at the Qt source code some more ... if all else fails, I can try putting the call to <code>QFile::remove()</code> in a <code>while</code> loop and call <code>std::yield</code> or <code>QApplication::processEvents()</code>.</p>
]]></description><link>https://forum.qt.io/post/836666</link><guid isPermaLink="true">https://forum.qt.io/post/836666</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Sat, 28 Feb 2026 08:45:49 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 08:16:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/andy314">@<bdi>Andy314</bdi></a><br />
Although I am sure this in itself is a useful recommendation, and may well solve the problem for whatever reason, did you really have the <em>same</em> problem where <code>QFile file(...); file.remove();</code> failed while <code>QFile::remove(...);</code> succeeded?  If so, do you have a repro?  This is what is strange here.  Because it seems like that would only be the case depending on what you had done with your <code>QFile file</code> instance....</p>
]]></description><link>https://forum.qt.io/post/836665</link><guid isPermaLink="true">https://forum.qt.io/post/836665</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 28 Feb 2026 08:16:32 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Sat, 28 Feb 2026 08:08:41 GMT]]></title><description><![CDATA[<p dir="auto">I had the same problem.  Simple close() of the database solved it.</p>
]]></description><link>https://forum.qt.io/post/836664</link><guid isPermaLink="true">https://forum.qt.io/post/836664</guid><dc:creator><![CDATA[Andy314]]></dc:creator><pubDate>Sat, 28 Feb 2026 08:08:41 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Fri, 27 Feb 2026 17:45:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a><br />
If you say you can literally put <code>QFile::remove(x)</code> where you have <code>x.remove()</code> and it works then I would look at/show what you exactly have done with your <code>QFile</code>.</p>
<p dir="auto">Also, assuming you are using <code>QSqlDatabase</code>, have you done <a href="https://doc.qt.io/qt-6/qsqldatabase.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">what you're not supposed to do</a> and kept a member variable copy of it?</p>
]]></description><link>https://forum.qt.io/post/836657</link><guid isPermaLink="true">https://forum.qt.io/post/836657</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 27 Feb 2026 17:45:12 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Fri, 27 Feb 2026 15:31:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/igkh">@<bdi>IgKh</bdi></a> and <a class="plugin-mentions-user plugin-mentions-a" href="/user/kent-dorfman">@<bdi>Kent-Dorfman</bdi></a> : Thank you, this is very helpful information. My application does call <code>QSqlDatabase::removeDatabase()</code> on the connection before it tries to delete the file, but maybe I need to also call <code>QApplication::processEvents()</code>?</p>
<p dir="auto">The tip about calling <code>GetLastError()</code> is a good one in situations like this. I will test this again before marking it solved.</p>
]]></description><link>https://forum.qt.io/post/836656</link><guid isPermaLink="true">https://forum.qt.io/post/836656</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Fri, 27 Feb 2026 15:31:57 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Fri, 27 Feb 2026 14:45:52 GMT]]></title><description><![CDATA[<p dir="auto">May or may not be relevant but in general terms when you create a DB object handle to a database the DB file becomes (open) for the duration of the DB handle.  Make sure you are destroying the DB handle before attempting to remove the database file.</p>
<p dir="auto">Yeah, what IgKh said above...</p>
]]></description><link>https://forum.qt.io/post/836653</link><guid isPermaLink="true">https://forum.qt.io/post/836653</guid><dc:creator><![CDATA[Kent-Dorfman]]></dc:creator><pubDate>Fri, 27 Feb 2026 14:45:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Fri, 27 Feb 2026 14:04:57 GMT]]></title><description><![CDATA[<p dir="auto">The static <code>QFile::remove</code> is <a href="https://github.com/qt/qtbase/blob/100639bfdb09d603bc4fdec88266d70f44d52b2b/src/corelib/io/qfile.cpp#L455" target="_blank" rel="noopener noreferrer nofollow ugc">literally defined</a> as <code>return QFile(fileName).remove()</code>. And the non-static version closes the file by itself if it is open.</p>
<p dir="auto">There is no difference in mechanism, so any difference in behavior you observe may be a coincidence. There is not enough information in the post to determine. I'd check that the SQLite connection is completely closed and discarded before removal of the file; note that even on Linux where it is possible to unlink files with open descriptors doing so to SQLite databases is never safe -  the WAL or rollback journal file may lose association which can lead to database corruption,</p>
<p dir="auto">As for getting the error from the static version, you can call the Win32 API method <code>GetLastError</code> yourself, if you'd like.</p>
]]></description><link>https://forum.qt.io/post/836651</link><guid isPermaLink="true">https://forum.qt.io/post/836651</guid><dc:creator><![CDATA[IgKh]]></dc:creator><pubDate>Fri, 27 Feb 2026 14:04:57 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Fri, 27 Feb 2026 13:46:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Nothing is writing to the file, otherwise the static <code>QFile::remove()</code> would also fail (but it doesn't).</p>
]]></description><link>https://forum.qt.io/post/836649</link><guid isPermaLink="true">https://forum.qt.io/post/836649</guid><dc:creator><![CDATA[Robert Hairgrove]]></dc:creator><pubDate>Fri, 27 Feb 2026 13:46:15 GMT</pubDate></item><item><title><![CDATA[Reply to How to properly call QFile::remove() on Windows? on Fri, 27 Feb 2026 13:34:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/robert-hairgrove">@<bdi>Robert-Hairgrove</bdi></a> said in <a href="/post/836647">How to properly call QFile::remove() on Windows?</a>:</p>
<blockquote>
<p dir="auto">On Windows, I am getting an error stating that the process cannot access the file because it is in use by another process.</p>
</blockquote>
<p dir="auto">It is not clear when it happens:<br />
a) When SQLite tries to write to that file<br />
b) When you're trying to delete it</p>
<p dir="auto">If it is b) - is the file still in use by SQLite?</p>
]]></description><link>https://forum.qt.io/post/836648</link><guid isPermaLink="true">https://forum.qt.io/post/836648</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 27 Feb 2026 13:34:51 GMT</pubDate></item></channel></rss>