0X00002752

WSAENOTEMPTY (0X00002752): Fix the 'Directory Not Empty' Socket Error

Hitting WSAENOTEMPTY when your code tries to delete a non-empty directory? Here's why it happens and exactly how to fix it on Windows.

You're writing a network client or server on Windows, and boom—your socket call fails with WSAENOTEMPTY (0X00002752). The message says "Cannot remove a directory that is not empty." But you're not even touching directories. You're sending or receiving data. This error tripped me up the first time I saw it too, because the message is misleading. It usually appears when you're trying to bind a socket to a Unix domain socket path, or when you're cleaning up after a failed socket operation and your code tries to remove() a socket file that still has connections or leftover files in its directory.

What's actually happening

On Windows, Unix domain sockets are implemented as files. When you create a Unix socket, Windows creates a placeholder file in the filesystem. If your program crashes or you forget to close the socket before deleting that file, the file stays. When you try to delete the directory containing that file—or when your socket code tries to unlink the socket file while it's still in use—Windows throws WSAENOTEMPTY. It's Windows's way of saying "I can't clean up because this directory still has stuff in it."

The most common trigger: you're using AF_UNIX sockets in a cross-platform app. On Linux, you can unlink() a socket file even if it's active. On Windows, you can't. So your cleanup code that worked on Linux suddenly breaks on Windows.

Root cause, in plain English

Windows doesn't let you delete a file that's still open or a directory that isn't empty. Unix domain sockets are treated as files, so if your socket is still bound or connected, the file is locked. When your code tries to remove() the socket file or its parent directory, Windows refuses, and the Winsock layer reports 0X00002752.

Another scenario: you're using a temporary directory for sockets, and a previous run left stale socket files behind. Your new run tries to reuse that directory, but it's not empty because of the leftovers.

How to fix it

Here's the fix that works, and it's not just about catching the error—it's about preventing it.

  1. Close the socket before cleanup. Always call closesocket() (or close() if you're using the CRT) on the socket before attempting to remove the file. This releases the lock. Don't skip this step, even if you think the socket is already dead.
  2. Remove the socket file, not the directory. If you're trying to remove a directory that contains the socket file, first unlink the socket file specifically. On Windows, you need to use DeleteFile() or remove() on the socket path, but only after the socket is closed.
  3. Use a unique directory per run. If your app creates a temp directory for Unix sockets, include a process ID or a random string in the directory name. This avoids stale files from previous runs.
  4. If you still hit the error, list the directory contents. Check for leftover files. You can do this in a command prompt:
    dir /s /b C:\path\to\socket\directory

    If you see a file like socket.tmp, delete it manually or in code before retrying.
  5. Use SetHandleInformation() to mark the socket handle as non-inheritable. This prevents child processes from inheriting the handle and keeping the file locked. It's a rare edge case, but I've seen it happen with forked worker processes.

Here's a code snippet in C that does it right:

// After socket is closed
closesocket(sock);

// Now safe to remove the socket file
const char* path = "C:/temp/mysocket.sock";
if (remove(path) != 0) {
    // Check errno - if it's EACCES, the file is still locked
    // If it's ENOENT, the file doesn't exist - that's fine
}

Still failing? Check these

  • Antivirus or backup software might be holding the file open. Temporarily disable it to test.
  • Windows Defender's controlled folder access can block file deletion. Add your app to the allowed list.
  • If you're using a network share, the directory might be on a mapped drive with permissions issues. Move the socket to a local path.
  • Check if the socket is bound to a different process. Use netstat -ano | findstr :port to see which PID owns the socket, then close that process.

The real fix is to treat Unix domain socket files as first-class files on Windows. Close them before you clean up, and don't assume the directory is empty until you've verified it. Once you get this pattern down, you won't see 0X00002752 again.

Related Errors in Network & Connectivity
0XC00D1191 Fix NS_E_PDA_NO_LONGER_AVAILABLE (0XC00D1191) in 5 Steps 0XC000A081 Fix STATUS_LOST_WRITEBEHIND_DATA_NETWORK_SERVER_ERROR 0XC000A081 0XC0000201 STATUS_NETWORK_OPEN_RESTRICTION 0XC0000201 fix IPv6 Router Advertisements Not Working – Quick Fixes

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.