How to Delete Files in Python
Tags: python, programming, beginners
Published: 2024-01-17 20:00:00

<figcaption>Photo by <a href="https://unsplash.com/@rwlinder?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Robert Linder</a> on <a href="https://unsplash.com/photos/a-row-of-trash-cans-sitting-next-to-a-brick-wall-JLoE-DntHtY?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>
  </figcaption>

Like, ***lots*** and lots of files...

---

I work as a web developer for a CRM that caters primarily to automotive dealerships. We receive thousands of customer leads each day from dozens of advertising sources.

We've recently realized that we have grown to such a size, that having thousands and thousands of email copies hanging out on our servers was starting to drag down on some performance times. So we decided to delete all emailed leads older than two weeks. I volunteered to complete this task twice monthly.

Friends, I was doing this manually. In other words, I would highlight 5,000 or so emails in the file window at a time and hit `DELETE`. And then I would have to delete them again by emptying the recycle bin.

This task would often take me an hour to complete.

![Michael Scott from 'The Office' facepalming](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExd2pmaGM1aGt0Y25kbWlscGZmajNmMnlvZ3czYW91NWFxd3YybmoxMiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/whQCarjn5Jv1Ktq2HH/giphy.gif)

One day, I asked myself, "Can I write a program that can do this for me while I ... *not* do this??"

Fortunately, the answer was yes.

---

There are several ways to safely and quickly delete files in Python.

####1. Using os.remove()
The <a href="https://docs.python.org/3/library/os.html" target="_blank">OS module</a> contains a few methods that can help you delete a file (or many files). The `os.remove()` function permanently deletes a file from the server.

```
import os

os.remove('name_of_file.txt')
```
It's always good practice to check if the file exists before trying to delete it. Otherwise, you'll run into a `FileNotFoundError` exception.

```
import os

my_file = 'name_of_file.txt'

## Make sure file exists
if os.path.exists(my_file):
    os.remove(my_file)
```

####2. Using os.unlink()
Similarly to `os.remove()`, `os.unlink()` removes one file permanently. Checking that the file exists before trying to delete it is important here, as well.
```
import os

file_path = '/path_to_my_file'

if os.path.exists(file_path):
    os.unlink(file_path)

```
Note that `os.unlink()` is only available in Unix-based systems like macOS and Linux. It may not work in Windows systems.

####3. Deleting multiple files at once

If you have more than one file to delete (i.e., a proverbial boatload), Python offers a few options.

The `os.listdir()` function retrieves all files and directories within a specified directory. We can then manipulate that list and iterate over each file and delete.

```
import os

file_path = '/path_to_my_file'

for file in os.listdir(file_path):
    if os.path.isfile(os.path.join('file_path', file)):
        os.remove(os.path.join('file_path', file))
```

Python's `glob` module is a little more robust, in that it finds files that match a specified pattern. For example, to remove all .png files from a folder that contains several different types of files, we could do this:
```
import os
import glob

file_path = '/path_to_my_file'

pattern_match = '*.png'

## Get a list of files that match our desired pattern
file_paths = glob.glob(os.path.join(file_path, pattern_match))

for file in file_paths:
    if os.path.exists(file):
        os.remove(file)

```

####4. Deleting directories

You might need to delete directories, in addition to individual files. Python can help with that, too.

If the directory is empty, the `os.rmdir()` function can delete it.
```
import os

dir_path = '/path_to_my_dir'

os.rmdir(dir_path)
```

If the directory is not empty when calling the `os.rmdir()` function, an `OSError` will be raised. So placing the call to delete a directory within a try/except block can help with this.
```
import os

dir_path = '/path_to_my_dir'

try:
    os.rmdir(dir_path)
except OSError as error:
    print (error)
```
If the directory you want to delete is not empty, Python's <a href="https://docs.python.org/3/library/shutil.html" target="_blank">shutil module</a> works well in this situation.

The `shutil.rmtree()` function can delete an empty or non-empty directory and all of its contents. As with the `os.rmdir()` function, it's best to place calls to `shutil.rmtree()` within try/except blocks, to handle various errors.
```
import shutil

dir_path = '/path_to_my_dir'

try:
    shutil.rmtree(dir_path)
except Exception as error:
    print (error)

```
<figcaption>With all of the above mentioned methods, please remember that these files and folders are deleted permanently. So back up your data or move them to a different location if you're not sure whether you will need these files in the future.</figcaption>
---
Python has saved me hours of time each month, now that I can delete thousands of files while I complete other tasks. Hopefully, my painstaking experience can serve as a reminder to take a close look at your daily tasks and ask yourself which ones could be automated. You might be pleasantly surprised how much time and effort you can spend on more exciting things.

![Michael Scott from 'The Office' dancing happily](https://media.giphy.com/media/l0amJzVHIAfl7jMDos/giphy.gif)

What tasks have you automated? Do you have a different preferred way to delete lots and lots of files that wasn't mentioned in this article? Let me know in the comments below!