CopySpec delete first

I have a use case where I want to copy into a directory, but what I’m copying will be different each time (strongly hashed filenames), and I don’t want old files to accumulate. I was hoping that CopySpec had a “deleteFirst” option to wipe the “into” location before performing the copy, but I don’t see anything promising. This is admittedly a mild mixing of concerns, but in this case it would save me having to declare the target path twice (or use a variable) for sequenced delete and copy steps. Using the copy task with a dependency on a delete task would be overkill, and the outputs are unique each time so I’m not worried about incremental tracking. Am I missing a more concise or idiomatic way to express this?

def destDir = 'destPath'
destDir.deleteDir()
copy {
	into destDir
	from '...'
}

I think the Sync task is what you are looking for.

That’s the semantic I was looking for, thanks. It’s not a method of project, so I apparently can’t just sync { ... } like I can with copy, but close enough.

Correct, it is its own task. If you want to do this within a custom task action, etc then you can use a combination of Project.delete() and Project.copy() as you’ve already noted.

Also have a look at injecting FileSystemOperations (see the note at the beginning of the section), it has all 3 methods you need.

(Edit: oops, 6 years later… I read “Feb '17” as this year’s 17th Feb.)