7-Zip Manual
Command line: commands
d (Delete) command
d (Delete) command
Deletes files from archive.
Example
7z d archive.zip *.bak -r
deletes *.bak files from archive
archive.zip .
Notes
7-Zip doesn't use the system wildcard parser. 7-Zip doesn't
follow the archaic rule by which *.* means any file. 7-Zip treats
*.* as matching the name of any file that has an extension. To process
all files, you must use a * wildcard.
Switches that can be used with this command
-bb (Set output log level)
-i (Include)
-m (Method)
-p (Set Password)
-r (Recurse)
-sns (Store NTFS alternate Streams)
-u (Update)
-w (Working Dir)
-x (Exclude)
See also
Commands:
a (Add) ,
u (Update)
Switches:
-spf (Use fully qualified file paths)
-spm (Require path separator mark for directory path)
-stl (Set archive timestamp from the most recently modified file)
-u (Update)
Notes from this site
Not part of the 7-Zip manual. Written by this site; examples checked with 7-Zip 26.04.
Quick examples
On Linux and macOS the official console binary is 7zz; on Windows it is 7z. Quote wildcards, otherwise the shell expands them before 7-Zip sees them and the command can fail or match the wrong names. d works on 7z, zip and tar archives.
cp archive.zip archive.zip.bak
7zz l archive.zip '*.bak' -r
7zz d archive.zip '*.bak' -r
7zz d archive.zip '*.bak' '*.tmp' -r
7zz d archive.zip subdir/
7zz d archive.zip 'my dir/file name.txt'
Make a copy first, because d changes the archive in place and cannot be undone. To replace a file instead of removing it, use u . List with l to preview what a pattern matches, then run the same pattern with d. subdir/ removes the folder, its contents and the folder entry itself; 'subdir/*' removes the contents but leaves an empty subdir entry. Names with spaces need quotes. On a solid .7z the archive file is rewritten as a whole (in my test it went from 282 to 248 bytes), so a big archive can take about as long as creating it.
Common mistakes: nothing deleted, .tar.gz, disk space
Leaving out -r: 7zz d archive.zip '*.bak' only looks at the top level of the archive. Nothing matches, nothing is removed, and it still prints "Everything is Ok" and exits with code 0 (see exit codes ), so a script cannot tell. Check that nothing is left: [ "$(7zz l -ba archive.zip '*.bak' -r | wc -l)" -eq 0 ] && echo clean prints clean when no entry matches any more.
Expecting to edit inside a .tar.gz: the gzip layer contains one item, the .tar file, so paths inside it do not match, and deleting that one item fails with E_INVALIDARG (exit code 2). Extract both layers, change the files and make a new archive.
Running low on disk space: 7-Zip builds a temporary new archive next to the old one and copies it over, so you need room for both. -w moves the temporary file to another folder.
Related pages