Delete Files from Archive Using 7-Zip CLI

This guide provides a straightforward walkthrough on how to delete files directly from an existing archive using the 7-Zip Command Line Interface (CLI). By using the built-in deletion command, you can modify archives like .7z, .zip, and other supported formats in place, eliminating the need to manually extract the contents, delete the target files, and recompress the archive.

The Basic Command Syntax

The 7-Zip CLI uses the d command to delete files from an archive. The basic syntax is:

7z d <archive_name> <file_to_delete>
  • 7z: Invokes the 7-Zip executable (ensure it is added to your system's PATH, or run it directly from the installation directory).
  • d: The command action specifying "delete".
  • <archive_name>: The relative or absolute path to the archive file.
  • <file_to_delete>: The name or path of the file inside the archive you wish to remove.

Examples

1. Deleting a Single File

To remove a file named document.pdf from an archive named backup.zip:

7z d backup.zip document.pdf

2. Deleting Multiple Files

You can specify multiple individual files separated by spaces:

7z d backup.zip file1.txt file2.log report.docx

3. Deleting Files with Wildcards

To delete all files matching a specific extension, use wildcards (*):

7z d backup.7z *.tmp

This command deletes all .tmp files located in the root of backup.7z.

4. Deleting Files in Subfolders

To delete a specific file located inside a subdirectory within the archive, specify the internal path:

7z d backup.zip assets/images/old_logo.png

To remove an entire directory inside the archive, specify the folder name:

7z d backup.zip assets/temp_folder

5. Recursive Deletion

To delete files matching a pattern across all subdirectories within the archive, use the -r (recursive) switch:

7z d backup.zip *.log -r

This command scans the entire archive tree and deletes every .log file found.

Important Notes

  • Non-destructive Formats: Some archive formats (such as .tar.gz) do not support direct in-place modifications due to how tar streams are compressed. This command works best with .7z and .zip containers.
  • Backups: The deletion action modifies the archive directly. It is recommended to create a backup copy of your archive before executing deletion commands with wildcards.