In WHM terminal. Delete backups from a folder.

By | February 4, 2025

Be extremely careful when deleting backups. Make absolutely sure you have identified the correct files and that you no longer need them before proceeding. Deleting backups can lead to irreversible data loss.

Here are the commands you can use, but use them with caution:

1. Deleting Specific Files:

Bash

rm /backup/filename1.tar.gz /backup/filename2.zip  # Replace with the actual filenames

This command deletes the specified files. You can list multiple files to delete at once.

2. Deleting Files Matching a Pattern:

Bash

rm /backup/*.tar.gz  # Deletes all .tar.gz files in /backup
rm /backup/cpmove-*  # Deletes all files starting with "cpmove-" in /backup

Use wildcards (*) with caution. This command deletes all files that match the specified pattern. Double-check the pattern to ensure you’re not deleting files you need.

3. Deleting Directories (and their contents):

Bash

rm -rf /backup/directory_name  # Replace with the actual directory name

The -r option makes the deletion recursive (deletes everything inside the directory), and the -f option forces the deletion (no prompts). This is the most dangerous command and should be used with extreme care. Make absolutely sure you are deleting the correct directory.

4. Using find for more complex filtering:

The find command offers more flexibility for locating and deleting files based on various criteria. Here are a few examples:

  • Deleting files older than a certain number of days:

Bash

find /backup -type f -mtime +30 -delete  # Deletes files older than 30 days
  • Deleting files matching a pattern and older than a certain number of days:

Bash

find /backup -type f -name "cpmove-*" -mtime +7 -delete # Delete cpmove files older than 7 days
  • Dry run (recommended before actually deleting):

You can use the -print option with find to see what would be deleted before actually deleting it:

Bash

find /backup -type f -name "cpmove-*" -mtime +7 -print # Shows which files would be deleted

Extremely Important Considerations:

  • Double-Check: Before running any rm command, especially with wildcards or the -rf option, list the files you are about to delete using ls with the same pattern. This is crucial to avoid mistakes. For example:

Bash

ls /backup/*.tar.gz  # List the .tar.gz files before deleting them
  • Backups of Backups: Consider having a separate backup of your backups. This adds an extra layer of protection against accidental deletion.
  • WHM Interface: WHM often provides a way to manage backups through its interface. This is generally safer than using the command line. Check your WHM Backup configuration.
  • Testing: If you’re unsure, test your rm or find commands on a test directory first before using them on your actual backups.
  • No Undo: Once you delete files with rm, they are usually gone for good. There’s no easy way to recover them unless you have other backups.

Strong Recommendation: Use the find ... -print (dry run) method first to verify what you’re about to delete. Only after you are absolutely sure should you replace -print with -delete. The potential for data loss is significant, so proceed with extreme caution. If you’re uncomfortable with the command line, use the WHM interface if available.

Leave a Reply

Your email address will not be published. Required fields are marked *