Help Center/ Scalable File Service Turbo/ FAQs/ SFS Turbo Deletion/ How Do I Delete a Large Number of Files or Folders from a Linux OS?
Updated on 2025-09-12 GMT+08:00

How Do I Delete a Large Number of Files or Folders from a Linux OS?

When there are a large number of files or folders, you can use the following methods to delete them to improve the deletion speed and prevent file system freezing.

rm Command

Delete all files in the current directory. The asterisk (*) indicates all files in the current directory.

rm -f *

find Command

  • Delete empty folders in a batch. -type d is used to filter items whose file type is directory, and -empty is used to filter empty folders.
    find . -type d -empty -delete
  • Delete files in a batch. The asterisk (*) is used to specify the file name.
    find . -name *.log -type f -delete
  • Forcibly delete non-empty folders in a batch. The asterisk (*) is used to specify the folder name.
    find . -type d -name * -exec rm -fr "{}" \;
  • Delete non-empty folders in a batch. The asterisk (*) and question mark (?) indicate different folder names. Use -o to separate folder names and -name.
    find . \( -name * -o -name ? \) -type d -exec rm -fr "{}" \;

rsync

  1. Install rsync.
    1. On Ubuntu or Debian, run the following command to install rsync:
      sudo apt install rsync
    2. On CentOS, RHEL, or Fedora, run the following command to install rsync:
      #For CentOS 7, RHEL 7, or an earlier version
      sudo yum install rsync
      #For CentOS 8, RHEL 8, or Fedora
      sudo dnf install rsync
  2. Create an empty folder dst_test and run the following command to delete the large number of files. This command is used to quickly clear the entire origin_test folder instead of deleting files individually.
    rsync -a --delete dst_test/ origin_test/

Python SDK

The following example code is used to delete all files in the test folder:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# test.py
import os
import time
stime=time.time()
for pathname,dirnames, filenames in os.walk('/home/username/test'):
     for filename in filenames:
         file=os.path. join(pathname, filename)
         os.remove(file)
ftime=time.time()
print ftime-stime

Run the following script:

python test.py