% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cache.clean.R
\name{cache.clean}
\alias{cache.clean}
\title{Cleans selected files from the current cache directory}
\usage{
cache.clean(older_than = NULL, interactive = TRUE)
}
\arguments{
\item{older_than}{A positive number indicating number of days (fractions allowed).
Files with older modification times than this will be deleted
after confirmation if \code{interactive = TRUE}.}

\item{interactive}{Logical.
\code{TRUE} indicates that confirmation will be required before files are deleted.
\code{FALSE} indicates that files will be deleted without requiring confirmation.
It is recommended to use \code{TRUE} except for carefully checked automated
processes.
Defaults to \code{TRUE}.}
}
\value{
\code{NULL} (invisibly). Primarily called to clean up the cache directory.
}
\description{
Functions in the package include options to write files to a cache directory,
set up with the \code{\link[=cache.setup]{cache.setup()}} function.
Obsoletely files might be created if the \code{name} parameter in a function call
is changed or if a function call is no longer being used.
The \code{cache.clean()} function provides a way to find files that have not be
modified in the last \code{older_than} days and lists the files for the user to
agree to delete or not.
}
\details{
The function is interactive if run in an interactive session with
\code{interctive = TRUE}.
In addition to deleting old files, the function will also optionally delete
empty directories and, if the top level cache directory is also empty,
then it will also optionally delete the cache directory and unset it.
The latter will only be done in interactive sessions with
\code{interactive = TRUE} to avoid breaking non-interactive code.

One way to clean only unused files is to run all current code and then
run \code{cache.clean()} with \code{older_than} set to something greater than the
number of days it takes for the code to run and less than when the code was
previously run. For example, if your code takes 5 minutes to run and you
previously ran it 2 days ago, you could run all your code and, when it has
finished, use \code{cache.clean(older_than = 1)}.

\code{semFromKeys} has no way to know what directories you have specified as the
cache in the past and cannot clean up unknown former cache directories.
Therefore, it is recommended that you use either the default location or
the same location for all models within the same project.
Either of these will enable easy detection, so that unneeded cached files
can be found and deleted.
}
\examples{
\dontrun{
  # Setup a cache directory
  cache.setup()
  # Now code with check = TRUE or save_out = TRUE will work, e.g.,
  # Create CFA keys
  keys0 <- c("grit_c", "grit_p", "hope_a", "hope_p")
  keys <- sapply(
    keys0, function(x) names(BFIGritHope)[grep(x, names(BFIGritHope))]
  )
  # Run models
  cfa_fit <- cfa.from.keys(
    keys, BFIGritHope, fit_save = TRUE, check = TRUE, save_out = TRUE
  )
  # Check that they are not estimated again.
  cfa_fit <- cfa.from.keys(
    keys, BFIGritHope, fit_save = TRUE, check = TRUE, save_out = TRUE
  )

  # The cache.clean lines are commented out so you do not inadvertently
  # delete any models created with your own code in the same cache directory.

  # cache.clean(30)  # Delete files not modified in the last 30 days.
  # cache.clean(60/86400)  # Delete files not modified in the last minute.
  # Delete all files matching those created by the package in the current
  # cache directory and subdirectories without confirmation.
  # cache.clean(0, interactive = FALSE)
}
}
\seealso{
\code{\link[=cache.setup]{cache.setup()}}
}
