Merge replication - Could not remove directory We are facing an issue with our replication server. We are currently setting up a new environment to replace the old replication server and we keep getting the following error.

Server XXX\YYY, Level 16, State 1, Procedure sp_MSreplremoveuncdir, Line 83
Could not remove directory '\\\XXXXXX\VTWSnapshot\unc\XXXX$YYYY\77_11\'. 
Check the security context of xp_cmdshell and close other processes that may 
be accessing the directory. (Source: MSSQLServer, Error number: 20015)


Sometimes when we run the job to generate a new snapshot, it fails and sometimes it succeeds. Since it sometimes succeeds, I'm sure it's not an issue with access or permissions, else it would always fail.



My second thought was that something is locking some of the files, not allowing SQL to delete anything from the folders. 
Sadly I only have permission on SQL and not on the server or file share, making this hard to check. Communication also goes a bit slow, me being in Belgium and the head office of their company being in America, that's why I'd like to get as much information as possible so I could ask everything at the same time.

[This may be coincidence, but I'll mention it anyway]

When we run the snapshot job X+1 times, it succeeds, with X being the number of subscriptions we have.

Meaning:
If we have 3 subscriptions A, B and C


Job fails the first time on folder A
Job fails on folder B
Job fails on folder C
Job succeeds


This kind of made me believe that the folders are somehow locked and the lock gets released after the job has run, but failed. The next time the job runs, it can continue with the next folder until they are all unlocked?

[/end of conspiracy]

Any help, thoughts, ideas are welcome.
The account under which xp_cmdshell is executing must have the proper access to the replication data share.

As a first step I would recommend check if xp_cmdshell is configured or not. If not, you have to enable for your purposes.


  -- To allow advanced options to be changed.
  EXEC sp_configure 'show advanced options', 1;
  GO
  -- To update the currently configured value for advanced options.
  RECONFIGURE;
  GO
  -- To enable the feature.
  EXEC sp_configure 'xp_cmdshell', 1;
  GO
  -- To update the currently configured value for this feature.
  RECONFIGURE;
  GO