Test::File::Cleaner - Automatically clean up your filesystem after tests


  # Create the cleaner
  my $Cleaner = Test::File::Cleaner->new( 'file_dmz' );
  # Do some tests that create files
  touch 'file_dmz/foo';
  # Cleaner cleans when it is DESTROYed
  # Alternatively, force an immediate clean up


When writing file-related testing code, it is common to end up with a number of files scattered all over the testing directories. If you are running the test scripts over and over these leftover files can interfere with subsequent test runs, and so they need to be cleaned up.

This clean up code typically needs to be done at END-time, so that the files are cleaned up even if you break out of the test script while it is running. The code to do this can get long and is labourious to maintain.

Test::File::Cleaner attempts to solve this problem. When you create a Cleaner object for a particular directory, the object scans and saves the contents of the directory.

When the object is DESTROYed, it compares the current state to the original, and removes any new files and directories created during the testing process.


new $dir

Creates a new Test::File::Cleaner object, which will automatically clean when it is destroyed. The cleaner is passed a directory within which it will operate, which must exist.

Since this is intended to be used in test scripts, it will die on error. You will not need to test the return value.


The path accessor returns the current root path for the object. The root path cannot be changed once the Test::File::Cleaner object has been created.


Calling the clean method forces a clean of the directory. The Cleaner will scan it's directory, compare what it finds with it's original scan, and then do whatever is needed to restore the directory to its original state.

Returns true if the Cleaner fully restores the directory, or false otherwise.


The reset method assumes you want to keep any changes that have been made, and will rescan the directory and store the new state instead.

Returns true of die on error


Bugs should be submitted via the CPAN bug tracker, located at

For other issues, or commercial enhancement or support, contact the author..


Adam Kennedy <>


Thank you to Phase N Australia ( ) for permitting the open sourcing and release of this distribution as a spin-off from a commercial project.


Copyright 2004 - 2007 Adam Kennedy.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.