Recovering Deleted Files
How do you get deleted files back?
PROBLEM
How do you get deleted files back?
SOLUTION
At the command line:
To restore a file that's been deleted in the depot, sync to the last good revision of the file, add it, and submit it.
For example, if foo.c was deleted at revision 26, you'd
restore it with:
p4 sync foo.c#25
p4 add foo.c
p4 submit
Or, say that when you
submitted changelist 300 you accidentally deleted all the files
in the //depot/main/src directory tree.
To restore them, use:
p4 sync //depot/main/src...@299
p4 add //depot/main/src/...
p4 submit
In the P4V GUI:
- Ensure that deleted files are shown in the Depot View pane (go to the View menu, select the Filter Depot menu and click on Show Deleted Depot Files)
- Locate the file in the Depot View pane
- Right click on the file and select "Get Revision"
- In the "Get Revision" dialog box, click on the radio button for "Specify revision using:" and leave "Revision" in the drop down box
- In the empty box to the right of the "Revision" drop down item, type in the number of the last good revision of the file
- Click "Get Revision"
- In the Depot View pane, right click on the file and select "Mark for Add"
- In the Depot View pane, right click on the file and select "Submit"
- Fill in the changelist description and click "Submit"
Release 98.2 and prior releases
If your Perforce Server is at Release 98.2 or earlier, restoring deleted files takes a few more steps. First, you need to sync to the undeleted revisions, then sync to the head revisions without removing the files. To sync without removing deleted files, you can either make copies of the files beforehand, or you can use p4 flush, as the example below demonstrates. Once you've done this you can use p4 add to add the files back.
For example, say that when you
submitted changelist 300 you accidentally deleted all the files
in the //depot/main/src directory tree.
The undeleted revisions are still in the depot, of course,
but the head revisions are marked as deleted.
To "undelete" those files, do the following:
-
Sync to the undeleted revisions. In this case, the files were undeleted
before changelist 300, so you can sync to @299:
p4 sync //depot/main/src/...@299
-
Use p4 flush or p4 sync -k to
sync to the head revisions without
removing the deleted files in your workspace:
p4 sync -k //depot/main/src/...
-
Run p4 add on
all the files in your workspace to open the restored files
for add.
-
On Unix, cd to the directory under which the files are found,
and use find to list all files:
find . -type f -print | p4 -x - add
-
On Windows platforms, you can do the equivalent by
selecting the directory in a File Manager or Explorer pane and
dragging it to your Pending Changelists pane in P4Win.
Or you can use this MS-DOS command:
dir/s/b | p4 -x - add
-
On Macintosh, use the files command from the MPW shell.
files -f -q -r -s | p4 -x - add
This actually just opens the restored files for "add". Any files in your workspace that were not deleted in the depot will be rejected by p4 add.
-
On Unix, cd to the directory under which the files are found,
and use find to list all files:
-
Now submit your changelist:
p4 submit
While in the changelist editor form, look for lines listing spurious files (i.e., any junk files that happened to be in your workspace directory path) and remove them from the submit list.
When p4 submit completes, the previously deleted files will now be restored in the depot.
