Search all unreachable code files, from one or more Main Entry files, and delete them.
Updated 22 Sep 2021

View License

Search all unreachable code files, from a Main Entry file (or multiple entry files), and delete them.
Do you happen to have a large Matlab project with few entry points (MAIN1,MAIN2,..) ? Then there is a chance that a lot of scripts within your root are not needed by none of those entry points. This tool helps you clean up your code before releasing it.
Note: Dependencies in this script are very conservative by design: A calls B if the string "B" is mentioned anywhere in A with the following conditions:
1) once all spaces are removed the last character before 'name' is ' ' or '='
2) once all spaces are removed the first character after 'name' is '( '
More specifically
functioName1/scriptName1 is considered Reachable from MAIN.m if the string functioName1/scriptName1 appears in MAIN.m and obeys the 2 reules above.
This is extended recursively to all depth of "calls" (AdjacencyMatrix is propagated into TransitiveClosure). At that point in the 1st row all the reachable files from main Nr 1 are flagged.
KILLORPHANFILES() starts-up a number of user dialogues consisting of these steps
- select MAIN file
- select ROOT folder
- LIST all MAIN-unreachable files in ROOT
- SELECT which ones / CONFIRM deletion
% - A list of all filenames within root is created,
% with MAIN1,MAIN2,.. in position 1,2,...
% - Make dependency table:
% A(i,j) = "name(j) appears in file(i)" (even within a comment)
% this way also commented code is protected (desired)
% - Propagate dependency (transitive closure of adjacency matrix A)
% If i mentions(needs) j and j mentions k then i needs k:
% i --> j --> k ==> i --> k
% A(i,j)==1
% A(j,k)==1 ==> A(i,k) = 1
% - A(1,:) contains all nodes called by MAIN1 within selected ROOT folder.
% - A(2,:) contains all nodes called by MAIN2 within selected ROOT folder.
% - Intersection of {~A(1,:),~A(2,:)} is the unreachable set
% by the union of MAIN1,MAIN2. Meaning both MAIN1,MAIN2 will run after
% clean up is completed

Cite As

Massimo Ciacci (2024). killOrphanFiles() (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!


Version Published Release Notes

updated comment

bug for multiple main entry points found and fixed.

(1)Fixed dependency check errors. A function name is being invoked if [X]name[Y] satisfy Y='(' and X terminates with a blank.
(2) Fix comment detection: A line of code is a comment if all chars before the first '%' are spaces.

- Following suggestion from Carl Witthoft, files are now moved to a folder called "__Orphans" created inside the user selected root folder. It is up to the user to actually delete the files.

User can now choose to ignore comments (a commented call is not a call). Ignoring comments deletes more orphans.
Should work also in 2011b, the new version was tested on 2015b.

Improved functionality, to support multiple entry points.

Changed screenshot to a less distracting one.

- Updated description about the weak definition of 'dependency'. Note however that this method will not miss any true dependency to the best of my knowledge. And it does not require special code toolboxes either.

- Added info in help section
- Added plots of graphs before and after pruning.

Added more info in summary

changed few strings