I need to delete a file from the target machine before an execution of my custom action. In case unsuccessful result of action I should rollback all the changes. So I should replace file to the temp directory, and on rollback move the file back, and clear the temp.
How can i do this?
I don't think you SHOULD do this. Windows Installer will do this for you.
Just schedule your custom action after RemoveFiles, condition it appropriately (e.g. run on uninstall only, it depends on your requirements) and make sure it is deferred. And that's it! If the uninstall is rolled back, Windows Installer knows how to rollback RemoveFiles action, and your file will be back there.