Skip to main content
When developing for Runpod serverless, it’s crucial to manage resources efficiently. The Runpod SDK provides a clean() function to help you remove temporary files and folders after processing. This guide will show you how to use this cleanup utility effectively.

The clean() Function

The clean() function is part of Runpod’s serverless utilities. It helps maintain a clean environment by removing specified folders and files after a job is completed. To use it, import the function from the Runpod serverless utilities:

Default Behavior

By default, clean() removes the following:
  • input_objects folder
  • output_objects folder
  • job_files folder
  • output.zip file

Using clean() in Your Handler

Here’s an example of how to incorporate the clean() function in your AI model handler:
In this example, clean() is called after the model processing is complete, ensuring that temporary files and folders are removed.

Custom Cleanup

You can also specify additional folders to be removed by passing a list to the clean() function:

Testing your Handler with Cleanup

To test your handler with the cleanup function:

Best Practices

  1. Call clean() at the end of your handler to ensure proper cleanup.
  2. Use try-except blocks to handle any errors during cleanup.
  3. Be cautious when adding custom folders to the cleanup list.
  4. Consider logging cleanup actions for debugging purposes.
By implementing the clean() function in your handlers, you ensure that each job starts with a clean slate, preventing potential issues caused by leftover files from previous runs.