Forum Discussion

LennSar's avatar
2 years ago

delete or clear excel xlsx file

Hi there,

I use a xlsx file to basically buffer some reference data. In the first step I generate that data, write it to a xlsx file and later get that data back from the xlsx file to compare it against the response of a Rest get request. 
To make sure that I only have the data generated in the current run I delete the files in an initial step using this groovy script:

def prjPath = context.expand('${projectDir}')
new File("${prjPath}/reports.xlsx").delete()
new File("${prjPath}/reportDates.csv").delete()

I can see from a command line and the explorer that the file is deleted but once I access it at the end of the above described process again the file contains the old data and the new data is appended to the data from the previous ran.

I assume this is some funny Windows magic which is caused by Windows not really deleting the file and by generating it with the 'Append' check box activated I basically retrieve the file from the trash and append the new data to it? 

Therefore, I would like to ask you whether you know of any good way to either REALLY delete these files or clear the content of the files.


Thanks a lot!

  • I took the opportunity to ask Nicolas Chara during a different service request about this issue and he pointed me towards setup and teardown scripts. This seems to do the trick which I assume is because it runs before the initialization procedure of the Test Case. 

    For later reference as I had some trouble finding it: The TestCase specific setup and tear down scripts can be found when clicking on the TestCase than shows TestSteps and than on the bottom Setup Script

5 Replies

  • richie's avatar
    Community Hero

    Hey LennSar 


    I tend to use the following java in a groovy step to clear the contents of a file - so the file remains but the contents are blatted out.

    def content = ''
    new File("D:/CA/test.txt").withWriter { writer ->


    Dunno if this is what you need?


    nice one,



  • Hi richie,


    thanks for the help. The script does work well if I run the groovy script step alone. But as soon as I run it in a testcase nothing happens. I had the same issue with my above script. If I run it alone, everything works as expected. If I run the script within a TestCase I can see in the explorer how the file dissappears but after my data sink is done with it, the old data is there as well and the new data is just appended. Any suggestion what could cause this effect? 
    Could the problem be that the data sink is writing the data from it's log and not dumping the log when the test case is restarted?
    I only have the problem with excel. Not with csv. Unfortunately csv is not an option in this case.

    The script also works as expected when run in Step-by-Step Run Mode. I think the excel file is just heavily cashed and that cashing might be buggy? I just realised the file doesn't even exist when my groovy script is reached because it seams to actually be written to the HD after the test case is finished. Here is a screenshot to clarify this:





    • richie's avatar
      Community Hero
      Hey LennSar,

      You say this is peculiar to xls, but not text files?

      Just a guess on my part, but i dont think this is a ReadyAPI issue....i could be wrong, but you never know.
      ReadyAPI as far as im aware doesnt cache this sort of stuff....just wsdls, etc, to speed things up a little.

      Have a go at accessing the office/excel file recovery/temp file options and disable them, restart your machine (you shouldnt need to, to make a registry change, but do it anyway), then reboot and re-execute.

      You might wanna check on reboot your changed settings havent been reverted by some Group Policy rule.


      • LennSar's avatar

        Hi richie,


        thanks for the suggestion. Sounded promising but unfortunately disabling the auto recovery doesn't change the behaviour. 


        Just to clarify I'll describe the test case procedure again:

        1) Delete files (I can see this happening in the command line and / or windows explorer)

        2) generate data

        3) write data using data sink

        4) loop back to 2 for x times

        5) done (I can now see the xlsx file appearing in the command line and / or windows explorer. NOT BEFORE THIS STEP)

        any further suggestions would be highly appreciated! Thanks!