Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a java application that:

  1. Reads a .bak file from a windows fileshare and restores it in SQL Server on a remote windows server

  2. Does various modifications on the database restored from the .bak file in step 1

  3. Backup the resulting database to a temporary .bak file (called somename.bak_tmp) on a windows fileshare

  4. Renames somename.bak_tmp to somename.bak (in the same directory on the same windows fileshare)

All the above steps works fine sometimes... I modified the rename code at some point which basically does this:

executeSql("master..xp_cmdShell 'rename "+tmpFile+" "+newName+"'");

to use the full path as newName. As a result the renaming failed. But now I cannot remove the somename.bak_tmp from the fileshare. Actually I cannot even see it (only the parent folder that contains it).

I have tried to delete the restored database from SQL Server but I still cannot delete the folder containing somename.bak_tmp. I get an Access Denied if I try to open it. If I right click the folder it says this on the owner tab:

Unable to display current owner.

And I cannot change the owner to Administrator even though I am logged in as administrator. Any ideas on what have happened to the folder during the failed rename and how to remove it?

share|improve this question
can you try running a command prompt on the machine that hosts the fileshare, by right clicking cmd and saying "run as admin" and trying to rename the file? – WrinkleFree Dec 24 '12 at 10:51
I have tried to open CMD in admin mode and tried to delete the folder: 'rmdir folder /S' but get Access is denied. Samen message with the rename command. – u123 Dec 26 '12 at 9:34
Can you try this? Right-click on folder -> Properties Security Tab, Click on Advanced button, Owner tab, Edit.., select "Administrators" in "Change owner to" and mark the checkbox "replace owner on subcontainers and objects" – WrinkleFree Dec 26 '12 at 10:28
I have tried that and I just get "Access Denied". I have also tried to delete (and close connections) the restored database from the mssql server but I still cannot remove the .bak file. The only way to remove it is to terminate the SQL Server Service which is not an option during regular hours. – u123 Dec 29 '12 at 7:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.