Can not delete folder item

I use this code to delete a file in the application data folder:

dim RMFile as FolderItem
dim f as FolderItem
f = SpecialFolder.ApplicationData.Child(app.ApplicationNameMBS)
RMFile = f.Child(MyDB.DatabaseFile.Name)

if RMFile.Exists then
RMFile.Exists.Delete

if RMFile.LastErrorCode > 0 then
  MsgBox "Cannot delete Database in folder: " + RMFile.NativePath
end if

else
MsgBox "Database file not found: " + RMFile.NativePath
end if

When I run in debug mode, there is no error, but the file was not removed. I have the same problem when I try to copy the file from the resource folder into application data. The file was not copied. Only when I put the database file on my desktop with finder and change my code to SpecialFolder.Desktop, then the file will be deleted. When I try to rename the database file in SpecialFolder.ApplicationData.Child(app.ApplicationNameMBS) it works and rename my file from database.rsd to database.db.

[quote=121528:@Horst Jehle]if RMFile.Exists then
RMFile.Exists.Delete[/quote]

if RMFile.Exists then RMFile.Exists.Delete

I think this is where the error is. It should have triggered an exception, by the way. it should be RMFile.Delete, I think, by looking at
http://documentation.xojo.com/index.php/FolderItem.Delete

RMFile.Exists is simply a boolean flagging the existence of the file, not a valid FolderItem.

Or, have you made sure to close the database before attempting to delete the file ?

Sorry that was an error by writing in this forum. It is RMFile.Delete. Why can a rename this file and remove does not work. Why folder RMFile.LastErrorCode is not greater than zero?. Why I can remove this file when it is on my desktop?

Chances are it is all related to the file being busy. Maybe it takes a while for the software to release the file after you close the database. You may want to try renaming or deleting after some delay.

Hello Horst,

if you debug your project and set a breakpoint to the line RMFile.Delete, stops the debugger on the breakpoint?

if RMFile.Exists then
  RMFile.Delete
end if

Another tip I read in the forum is:

//use
if RMFile.LastErrorCode <> 0 then

//instead of if 
RMFile.LastErrorCode > 0 then

because the LastErrorCode can have negative values too.

The pathes for SpecialFolder.ApplicationData.Child(app.ApplicationNameMBS) could be different between your debug version and your build app, if your build app is sandboxed.

If have found the error:

When I use this:
dim RMFile as FolderItem
dim f as FolderItem
f = SpecialFolder.ApplicationData.Child(app.ApplicationNameMBS).
RMFile = f.child(“database.rsd”)
I can’t delete the file. Only when I use this code, it works:
dim RMFile as FolderItem
RMFile = SpecialFolder.ApplicationData.Child(app.ApplicationNameMBS).child(“databse.rsd”)

if RMFile.Exists then
RMFile.Delete
end if