Recovering Deleted Files

PROBLEM

How do I get deleted files back?


SOLUTION

At the command line:

To restore a file that was deleted in the depot, p4 sync to the last good revision of the file, p4 add it, and p4 submit it.

For example, if foo.c was deleted at revision 26, you restore it with:

p4 sync foo.c#25
p4 add foo.c
p4 submit

Or, if you submitted changelist 300 and accidentally deleted all the files in the //depot/main/src directory tree you can restore them with:

p4 sync //depot/main/src/...@299
p4 add //depot/main/src/...
p4 submit

In the P4V GUI:

  • Ensure that the 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
  • Context-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, context-click on the file and select "Mark for Add"
  • In the Depot View pane, context-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 sync without removing deleted files you can use p4 add to add the files back.

In the following example, 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:

  1. 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
    
  2. 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/...
    
  3. 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 in the Terminal window.
      files -f -q -r -s | p4 -x - add
      

    The p4 add command just opens the restored files for "add". Any files in your workspace that were not deleted in the depot are rejected by p4 add.

  4. Now submit your changelist:
    p4 submit
    

    While in the changelist editor form, look for lines listing spurious files (such as 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 are now restored in the depot.